Skip to content

Commit 9eba429

Browse files
authored
Merge pull request abranhe#18 from dieterpl/palindrome_checker
Added a palindrome checker
2 parents 454682b + be2110e commit 9eba429

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_check import *
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+
import re
10+
11+
def palindrome_check(s):
12+
s = re.sub(r'[^\w]', '', s)
13+
if len(s) < 2:
14+
return True
15+
if s[0].lower() != s[-1].lower():
16+
return False
17+
return palindrome_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.string import 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+
> Return True if string is 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+
import unittest
2+
3+
from allalgorithms.string import palindrome_check
4+
5+
6+
class TestSorting(unittest.TestCase):
7+
8+
def test_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)