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
22 changes: 19 additions & 3 deletions palindrome_checker.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
# palindrome_checker.py
import re

def is_palindrome(s):
return s == s[::-1]
# Normalize the string: remove non-alphanumeric characters and convert to lowercase
normalized_s = re.sub(r'[^A-Za-z0-9]', '', s).lower()

# Early exit for trivial cases
if len(normalized_s) <= 1:
return True

# Check for palindrome
left, right = 0, len(normalized_s) - 1
while left < right:
if normalized_s[left] != normalized_s[right]:
return False
left += 1
right -= 1

return True

if __name__ == "__main__":
word = input("Enter a word: ")
word = input("Enter a word or phrase: ")
print(f"{word} is a palindrome: {is_palindrome(word)}")