Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9eba429

Browse files
authored
Merge pull requestabranhe#18 from dieterpl/palindrome_checker
Added a palindrome checker
2 parents454682b +be2110e commit9eba429

File tree

6 files changed

+78
-1
lines changed

6 files changed

+78
-1
lines changed

‎allalgorithms/string/__init__.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from .palindrome_checkimport*
2+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# -*- coding: UTF-8 -*-
2+
#
3+
# Checks if string is a palindrome
4+
# The All ▲lgorithms library for python
5+
#
6+
# Contributed by: dieterpl
7+
# Github: @dieterpl
8+
#
9+
importre
10+
11+
defpalindrome_check(s):
12+
s=re.sub(r'[^\w]','',s)
13+
iflen(s)<2:
14+
returnTrue
15+
ifs[0].lower()!=s[-1].lower():
16+
returnFalse
17+
returnpalindrome_check(s[1:-1])

‎changelog.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,7 @@ Added:
3232
- Insertion Sort
3333
- Pigeonhole Sort
3434
- Selection Sort
35-
- Stooge Sort
35+
- Stooge Sort
36+
37+
-###String
38+
- Palindrome Checker

‎docs/string/palindrome-check.md‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#Palindrome Check
2+
3+
A palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward, such as madam or racecar or the number 10201. (Wikipedia)
4+
5+
##Install
6+
7+
```
8+
pip install allalgorithms
9+
```
10+
11+
##Usage
12+
13+
```py
14+
from allalgorithms.stringimport palindrome_check
15+
16+
str="10201"
17+
18+
print(palindrome_check(str)
19+
# -> True
20+
21+
str="test"
22+
23+
print(palindrome_check(str)
24+
# -> False
25+
```
26+
27+
## API
28+
29+
### palindrome_check(string)
30+
31+
> ReturnTrueif stringis a palindrome,False otherwise
32+
33+
##### Params:
34+
35+
-`string`: Input String
36+

‎readme.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ print(binary_search(arr, 3))
7070
-[Pigeonhole Sort](https://python.allalgorithms.com/sorting/pigeonhole-sort)
7171
-[Selection Sort](https://python.allalgorithms.com/sorting/selection-sort)
7272
-[Stooge Sort](https://python.allalgorithms.com/sorting/stooge-sort)
73+
-###String
74+
-[Palindrome Check](https://python.allalgorithms.com/string/palindrom-check)
7375

7476
#Related
7577

‎tests/test_string.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
importunittest
2+
3+
fromallalgorithms.stringimportpalindrome_check
4+
5+
6+
classTestSorting(unittest.TestCase):
7+
8+
deftest_palindrome_check(self):
9+
self.assertEqual(True,palindrome_check("a"))
10+
self.assertEqual(True,palindrome_check("10201"))
11+
self.assertEqual(False,palindrome_check("test"))
12+
self.assertEqual(True,palindrome_check("Mr. Owl ate my metal worm"))
13+
self.assertEqual(True,palindrome_check("Was it a car or a cat I saw?"))
14+
self.assertEqual(False,palindrome_check("How are you?"))
15+
16+
if__name__=="__main__":
17+
unittest.main()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp