Skip to content

Commit 93e176d

Browse files
author
dieterpl
committed
Added a palindrome checker
1 parent ee71bfe commit 93e176d

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_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: 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.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+

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy