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

Commit93e176d

Browse files
author
dieterpl
committed
Added a palindrome checker
1 parentee71bfe commit93e176d

File tree

5 files changed

+85
-0
lines changed

5 files changed

+85
-0
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,16 @@ Added:
2828
- Pigeonhole Sort
2929
- Selection Sort
3030
- Stooge Sort
31+
32+
#Version`0.0.2`
33+
34+
Date: October 10, 2018
35+
36+
###Algorithms:
37+
38+
Added:
39+
40+
-###String
41+
- Palindrome Checker
42+
43+

‎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+

‎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