Skip to content
Open
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
23 changes: 6 additions & 17 deletions strings/palindrome.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@


def is_palindrome(s: str) -> bool:
"""
Return True if s is a palindrome otherwise return False.

>>> all(is_palindrome(key) is value for key, value in test_data.items())
True
"""
if not isinstance(s, str):
return False

start_i = 0
end_i = len(s) - 1
Expand All @@ -38,21 +34,14 @@ def is_palindrome(s: str) -> bool:


def is_palindrome_traversal(s: str) -> bool:
"""
Return True if s is a palindrome otherwise return False.
if not isinstance(s, str):
return False
if len(s) < 2:
return True

>>> 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))


Expand Down