I am writing a python3 implementation of palindromes checker.
import string
def is_palindrome(text):
"""A string of characters is a palindrome if it reads the same forwards and
backwards, ignoring punctuation, whitespace, and letter casing"""
# implement is_palindrome_iterative and is_palindrome_recursive below, then
# change this to call your implementation to verify it passes all tests
assert isinstance(text, str)
return is_palindrome_iterative(text)
# return is_palindrome_recursive(text)
def is_palindrome_iterative(text):
word = text
# if the word[index] is not the same, return False
# iteration through the index from the half of the length
# doing it through
for index in range(len(word)//2):
if word[index] != word[-1-index]:
return False
return True
def main():
import sys
args = sys.argv[1:] # Ignore script file name
if len(args) > 0:
for arg in args:
is_pal = is_palindrome(arg)
result = 'PASS' if is_pal else 'FAIL'
str_not = 'a' if is_pal else 'not a'
print('{}: {} is {} palindrome'.format(result, repr(arg), str_not))
else:
print('Usage: {} string1 string2 ... stringN'.format(sys.argv[0]))
print(' checks if each argument given is a palindrome')
if __name__ == '__main__':
main()
I think it works well.
ran python3 palindromes.py 'ABA'
output is
PASS: 'ABA' is a palindrome
print(arg == arg[::-1])
\$\endgroup\$