forked from learning-zone/python-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanagram_of_palindrome.py
47 lines (30 loc) · 1.07 KB
/
anagram_of_palindrome.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
47
# Is the word an anagram of a palindrome?
# A palindrome is a word that reads the same forward and backwards (eg, "racecar", "tacocat"). An anagram is a rescrambling of a word (eg for "racecar", you could rescramble this as "arceace").
# Determine if the given word is a re-scrambling of a palindrome.
# The word will only contain lowercase letters, a-z.
def is_anagram_of_palindrome(word):
""" Is the word an anagram of a palindrome?
>>> is_anagram_of_palindrome("a")
True
>>> is_anagram_of_palindrome("ab")
False
>>> is_anagram_of_palindrome("aab")
True
>>> is_anagram_of_palindrome("arceace")
True
>>> is_anagram_of_palindrome("arceaceb")
False
"""
# Runtime: O(n)
word_dict = {}
for l in word:
word_dict['l'] = word_dict.get(l, 0) + 1
for letter in word_dict:
if word_dict[letter] % 2 != 0 and len(word) % 2 == 0:
return False
return True
if __name__ == '__main__':
import doctest
results = doctest.testmod()
if results.failed == 0:
print "ALL TESTS PASSED!"