From 93e176dd2079c0b1777d3aa0efbbc85344581e42 Mon Sep 17 00:00:00 2001 From: dieterpl Date: Thu, 11 Oct 2018 00:21:49 +0200 Subject: [PATCH 1/2] Added a palindrome checker --- allalgorithms/string/__init__.py | 2 ++ allalgorithms/string/palindrome_check.py | 17 +++++++++++ changelog.md | 13 +++++++++ docs/string/palindrome-check.md | 36 ++++++++++++++++++++++++ tests/test_string.py | 17 +++++++++++ 5 files changed, 85 insertions(+) create mode 100644 allalgorithms/string/__init__.py create mode 100644 allalgorithms/string/palindrome_check.py create mode 100644 docs/string/palindrome-check.md create mode 100644 tests/test_string.py 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 71baf9e..e40a1bf 100644 --- a/changelog.md +++ b/changelog.md @@ -28,3 +28,16 @@ Added: - Pigeonhole Sort - Selection Sort - Stooge Sort + +# Version `0.0.2` + +Date: October 10, 2018 + +### Algorithms: + +Added: + +- ### String + - Palindrome Checker + + 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/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() From 6669e314757bffce34bb358a2d6cd06d4c924f21 Mon Sep 17 00:00:00 2001 From: dieterpl Date: Thu, 11 Oct 2018 00:24:34 +0200 Subject: [PATCH 2/2] Added readme text --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index 1d5be4f..355a72a 100644 --- a/readme.md +++ b/readme.md @@ -67,6 +67,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