mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-24 18:08:39 +00:00
Compare commits
No commits in common. "a17791d022bdc942c8badabc52307c354069a7ae" and "200429fc4739c3757180635016614b984cfd2206" have entirely different histories.
a17791d022
...
200429fc47
@ -1156,6 +1156,7 @@
|
||||
* [Indian Phone Validator](strings/indian_phone_validator.py)
|
||||
* [Is Contains Unique Chars](strings/is_contains_unique_chars.py)
|
||||
* [Is Isogram](strings/is_isogram.py)
|
||||
* [Is Palindrome](strings/is_palindrome.py)
|
||||
* [Is Pangram](strings/is_pangram.py)
|
||||
* [Is Spain National Id](strings/is_spain_national_id.py)
|
||||
* [Is Srilankan Phone Number](strings/is_srilankan_phone_number.py)
|
||||
|
@ -58,8 +58,8 @@ class Node:
|
||||
The heuristic here is the Manhattan Distance
|
||||
Could elaborate to offer more than one choice
|
||||
"""
|
||||
dx = abs(self.pos_x - self.goal_x)
|
||||
dy = abs(self.pos_y - self.goal_y)
|
||||
dy = abs(self.pos_x - self.goal_x)
|
||||
dx = abs(self.pos_y - self.goal_y)
|
||||
return dx + dy
|
||||
|
||||
def __lt__(self, other) -> bool:
|
||||
|
41
strings/is_palindrome.py
Normal file
41
strings/is_palindrome.py
Normal file
@ -0,0 +1,41 @@
|
||||
def is_palindrome(s: str) -> bool:
|
||||
"""
|
||||
Determine if the string s is a palindrome.
|
||||
|
||||
>>> is_palindrome("A man, A plan, A canal -- Panama!")
|
||||
True
|
||||
>>> is_palindrome("Hello")
|
||||
False
|
||||
>>> is_palindrome("Able was I ere I saw Elba")
|
||||
True
|
||||
>>> is_palindrome("racecar")
|
||||
True
|
||||
>>> is_palindrome("Mr. Owl ate my metal worm?")
|
||||
True
|
||||
"""
|
||||
# Since punctuation, capitalization, and spaces are often ignored while checking
|
||||
# palindromes, we first remove them from our string.
|
||||
s = "".join(character for character in s.lower() if character.isalnum())
|
||||
# return s == s[::-1] the slicing method
|
||||
# uses extra spaces we can
|
||||
# better with iteration method.
|
||||
|
||||
end = len(s) // 2
|
||||
n = len(s)
|
||||
|
||||
# We need to traverse till half of the length of string
|
||||
# as we can get access of the i'th last element from
|
||||
# i'th index.
|
||||
# eg: [0,1,2,3,4,5] => 4th index can be accessed
|
||||
# with the help of 1st index (i==n-i-1)
|
||||
# where n is length of string
|
||||
|
||||
return all(s[i] == s[n - i - 1] for i in range(end))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
s = input("Please enter a string to see if it is a palindrome: ")
|
||||
if is_palindrome(s):
|
||||
print(f"'{s}' is a palindrome.")
|
||||
else:
|
||||
print(f"'{s}' is not a palindrome.")
|
@ -1,7 +1,5 @@
|
||||
# Algorithms to determine if a string is palindrome
|
||||
|
||||
from timeit import timeit
|
||||
|
||||
test_data = {
|
||||
"MALAYALAM": True,
|
||||
"String": False,
|
||||
@ -35,25 +33,6 @@ def is_palindrome(s: str) -> bool:
|
||||
return True
|
||||
|
||||
|
||||
def is_palindrome_traversal(s: str) -> bool:
|
||||
"""
|
||||
Return True if s is a palindrome otherwise return False.
|
||||
|
||||
>>> all(is_palindrome_traversal(key) is value for key, value in test_data.items())
|
||||
True
|
||||
"""
|
||||
end = len(s) // 2
|
||||
n = len(s)
|
||||
|
||||
# We need to traverse till half of the length of string
|
||||
# as we can get access of the i'th last element from
|
||||
# i'th index.
|
||||
# eg: [0,1,2,3,4,5] => 4th index can be accessed
|
||||
# with the help of 1st index (i==n-i-1)
|
||||
# where n is length of string
|
||||
return all(s[i] == s[n - i - 1] for i in range(end))
|
||||
|
||||
|
||||
def is_palindrome_recursive(s: str) -> bool:
|
||||
"""
|
||||
Return True if s is a palindrome otherwise return False.
|
||||
@ -61,7 +40,7 @@ def is_palindrome_recursive(s: str) -> bool:
|
||||
>>> all(is_palindrome_recursive(key) is value for key, value in test_data.items())
|
||||
True
|
||||
"""
|
||||
if len(s) <= 2:
|
||||
if len(s) <= 1:
|
||||
return True
|
||||
if s[0] == s[len(s) - 1]:
|
||||
return is_palindrome_recursive(s[1:-1])
|
||||
@ -79,26 +58,9 @@ def is_palindrome_slice(s: str) -> bool:
|
||||
return s == s[::-1]
|
||||
|
||||
|
||||
def benchmark_function(name: str) -> None:
|
||||
stmt = f"all({name}(key) is value for key, value in test_data.items())"
|
||||
setup = f"from __main__ import test_data, {name}"
|
||||
number = 500000
|
||||
result = timeit(stmt=stmt, setup=setup, number=number)
|
||||
print(f"{name:<35} finished {number:,} runs in {result:.5f} seconds")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
for key, value in test_data.items():
|
||||
assert is_palindrome(key) is is_palindrome_recursive(key)
|
||||
assert is_palindrome(key) is is_palindrome_slice(key)
|
||||
print(f"{key:21} {value}")
|
||||
print("a man a plan a canal panama")
|
||||
|
||||
# finished 500,000 runs in 0.46793 seconds
|
||||
benchmark_function("is_palindrome_slice")
|
||||
# finished 500,000 runs in 0.85234 seconds
|
||||
benchmark_function("is_palindrome")
|
||||
# finished 500,000 runs in 1.32028 seconds
|
||||
benchmark_function("is_palindrome_recursive")
|
||||
# finished 500,000 runs in 2.08679 seconds
|
||||
benchmark_function("is_palindrome_traversal")
|
||||
|
Loading…
x
Reference in New Issue
Block a user