from collections import Counter
import string as ss
def check_pal_per(string):
string = string.lower()
hashset = set()
for s in string:
if s == " ":
continue
elif s not in hashset:
hashset.add(s)
else:
hashset.remove(s)
if len(hashset) == 0:
return True
elif len(hashset) % 2 !=0 and len(hashset) == 1:
return True
else:
return False
def clean(string):
return [c for c in string.lower() if c in ss.ascii_lowercase]
def check_pal_per_counter(string):
counter = Counter(clean(string))
return sum(c%2 for c in counter.values()) <= 1
test_cases = [
("aba", True),
("aab", True),
("abba", True),
("aabb", True),
("a-bba", True),
("a-bba!", True),
("Tact Coa", True),
("jhsabckuj ahjsbckj", True),
("Able was I ere I saw Elba", True),
("So patient a nurse to nurse a patient so", False),
("Random Words", False),
("Not a Palindrome", False),
("no x in nixon", True),
("azAZ", True),
]
func = [
check_pal_per,
check_pal_per_counter
]
Test Function
def runtest(func, test_cases):
for fun in func:
print(fun.__name__)
print("---------------")
for val, result in test_cases:
o = fun(val)
if o == result:
print("Passed ✓ for '%s'"%val)
else:
print("X Error for val '{}', expected: {}, got: {}".format(val, result, check_pal_per(val)))
print()