| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ee71bfe commit 93e176d
5 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + from .palindrome_check import * | ||
| 2 | + | ||
| Original file line number | Diff line number | Diff 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]) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -28,3 +28,16 @@ Added: | |||
| 28 | 28 | - Pigeonhole Sort | |
| 29 | 29 | - Selection Sort | |
| 30 | 30 | - 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 | + | ||
| Original file line number | Diff line number | Diff 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 | + | ||
| Original file line number | Diff line number | Diff 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() | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments