Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
abranhe merged 3 commits intoabranhe:masterfromdieterpl:palindrome_checker
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes fromall 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 deletionsallalgorithms/string/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
from .palindrome_check import *

17 changes: 17 additions & 0 deletionsallalgorithms/string/palindrome_check.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionchangelog.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsdocs/string/palindrome-check.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsreadme.md
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionstests/test_string.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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()

[8]ページ先頭

©2009-2025 Movatter.jp