Skip to content

Commit 69e9621

Browse files
1 algo for permutation palindrome
1 parent 6b53296 commit 69e9621

File tree

1 file changed

+42
-0
lines changed
  • Python/chapter01/1.1 - Is Unique

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/python3
2+
'''
3+
Palindrome Permutation: Given a string, write a function to check if it is a permutation of a palindrome.
4+
A palindrome is a word or phrase that is the same forwards and backwards. A permutation
5+
is a rearrangement of letters. The palindrome does not need to be limited to just dictionary words
6+
'''
7+
8+
import unittest
9+
10+
11+
def palperm(string):
12+
'''
13+
Function that checks if it is a permutation of a palindrome
14+
Args:
15+
string (str): a string to check for all palindrome permutations
16+
'''
17+
table = {}
18+
count = 0
19+
for letter in string.lower().replace(" ", ""):
20+
table[letter] = table.get(letter, 0) + 1
21+
22+
for key in table:
23+
if count > 1:
24+
return False
25+
if table[key] % 2 == 1:
26+
count += 1
27+
return True
28+
29+
30+
class Test(unittest.TestCase):
31+
def test1(self):
32+
input_string ="Tact Coa"
33+
input_string2 ="nick"
34+
input_string3 ="saippuakivikauppias"
35+
input_string4 = "iasppaukivikauppias"
36+
self.assertTrue(palperm(input_string))
37+
self.assertFalse(palperm(input_string2))
38+
self.assertTrue(palperm(input_string3))
39+
self.assertTrue(palperm(input_string4))
40+
41+
if __name__ == '__main__':
42+
unittest.main()

0 commit comments

Comments
 (0)