File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Python/chapter01/1.1 - Is Unique Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change
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 ()
You can’t perform that action at this time.
0 commit comments