Skip to content

Added a palindrome checker #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions allalgorithms/string/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .palindrome_check import *

17 changes: 17 additions & 0 deletions allalgorithms/string/palindrome_check.py
Original file line number Diff line number Diff line change
@@ -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])
5 changes: 4 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ Added:
- Insertion Sort
- Pigeonhole Sort
- Selection Sort
- Stooge Sort
- Stooge Sort

- ### String
- Palindrome Checker
36 changes: 36 additions & 0 deletions docs/string/palindrome-check.md
Original file line number Diff line number Diff line change
@@ -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

2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions tests/test_string.py
Original file line number Diff line number Diff line change
@@ -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()