diff --git a/allalgorithms/string/__init__.py b/allalgorithms/string/__init__.py new file mode 100644 index 0000000..6d8b504 --- /dev/null +++ b/allalgorithms/string/__init__.py @@ -0,0 +1,2 @@ +from .palindrome_check import * + diff --git a/allalgorithms/string/palindrome_check.py b/allalgorithms/string/palindrome_check.py new file mode 100644 index 0000000..a9a5991 --- /dev/null +++ b/allalgorithms/string/palindrome_check.py @@ -0,0 +1,17 @@ +# -*- coding: UTF-8 -*- +# +# Checks if string is a palindrome +# The All â–²lgorithms library for python +# +# Contributed by: dieterpl +# Github: @dieterpl +# +import re + +def palindrome_check(s): + s = re.sub(r'[^\w]', '', s) + if len(s) < 2: + return True + if s[0].lower() != s[-1].lower(): + return False + return palindrome_check(s[1:-1]) diff --git a/changelog.md b/changelog.md index 4b4fb67..bca1a83 100644 --- a/changelog.md +++ b/changelog.md @@ -32,4 +32,7 @@ Added: - Insertion Sort - Pigeonhole Sort - Selection Sort - - Stooge Sort \ No newline at end of file + - Stooge Sort + +- ### String + - Palindrome Checker \ No newline at end of file diff --git a/docs/string/palindrome-check.md b/docs/string/palindrome-check.md new file mode 100644 index 0000000..6e6cc7f --- /dev/null +++ b/docs/string/palindrome-check.md @@ -0,0 +1,36 @@ +# Palindrome Check + +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) + +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.string import palindrome_check + +str = "10201" + +print(palindrome_check(str) +# -> True + +str = "test" + +print(palindrome_check(str) +# -> False +``` + +## API + +### palindrome_check(string) + +> Return True if string is a palindrome, False otherwise + +##### Params: + +- `string`: Input String + diff --git a/readme.md b/readme.md index fe2ceb1..b25dfe4 100644 --- a/readme.md +++ b/readme.md @@ -70,6 +70,8 @@ print(binary_search(arr, 3)) - [Pigeonhole Sort](https://python.allalgorithms.com/sorting/pigeonhole-sort) - [Selection Sort](https://python.allalgorithms.com/sorting/selection-sort) - [Stooge Sort](https://python.allalgorithms.com/sorting/stooge-sort) +- ### String + - [Palindrome Check](https://python.allalgorithms.com/string/palindrom-check) # Related diff --git a/tests/test_string.py b/tests/test_string.py new file mode 100644 index 0000000..998daf0 --- /dev/null +++ b/tests/test_string.py @@ -0,0 +1,17 @@ +import unittest + +from allalgorithms.string import palindrome_check + + +class TestSorting(unittest.TestCase): + + def test_palindrome_check(self): + self.assertEqual(True, palindrome_check("a")) + self.assertEqual(True, palindrome_check("10201")) + self.assertEqual(False, palindrome_check("test")) + self.assertEqual(True, palindrome_check("Mr. Owl ate my metal worm")) + self.assertEqual(True, palindrome_check("Was it a car or a cat I saw?")) + self.assertEqual(False, palindrome_check("How are you?")) + +if __name__ == "__main__": + unittest.main()