-
Notifications
You must be signed in to change notification settings - Fork 22
/
palindromes.py
46 lines (35 loc) · 1.58 KB
/
palindromes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!python
# Hint: use string.ascii_letters (all letters in ASCII character set)
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):
# TODO: implement the is_palindrome function iteratively here
pass
# once implemented, change is_palindrome to call is_palindrome_iterative
# to verify that your iterative implementation passes all tests
def is_palindrome_recursive(text, left=None, right=None):
# TODO: implement the is_palindrome function recursively here
pass
# once implemented, change is_palindrome to call is_palindrome_recursive
# to verify that your iterative implementation passes all tests
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()