-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermutationPalindrome.py
68 lines (38 loc) · 1.33 KB
/
permutationPalindrome.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import unittest
def has_palindrome_permutation(the_string):
# Check if any permutation of the input is a palindrome
chars = list(the_string)
charMap = {}
for char in chars:
if char in charMap:
charMap[char] +=1
else:
charMap[char] = 1
odds = 0
for key in charMap.keys():
if not(charMap[key]%2 == 0):
odds +=1
if odds <=1 :
return True
return False
# Tests
class Test(unittest.TestCase):
def test_permutation_with_odd_number_of_chars(self):
result = has_palindrome_permutation('aabcbcd')
self.assertTrue(result)
def test_permutation_with_even_number_of_chars(self):
result = has_palindrome_permutation('aabccbdd')
self.assertTrue(result)
def test_no_permutation_with_odd_number_of_chars(self):
result = has_palindrome_permutation('aabcd')
self.assertFalse(result)
def test_no_permutation_with_even_number_of_chars(self):
result = has_palindrome_permutation('aabbcd')
self.assertFalse(result)
def test_empty_string(self):
result = has_palindrome_permutation('')
self.assertTrue(result)
def test_one_character_string(self):
result = has_palindrome_permutation('a')
self.assertTrue(result)
unittest.main(verbosity=2)