File tree Expand file tree Collapse file tree 1 file changed +24
-1
lines changed
Python/chapter01/1.4 - PalinPerm Expand file tree Collapse file tree 1 file changed +24
-1
lines changed Original file line number Diff line number Diff line change 6
6
'''
7
7
8
8
import unittest
9
-
9
+ import collections
10
10
11
11
def palperm (string ):
12
12
'''
@@ -26,6 +26,19 @@ def palperm(string):
26
26
count += 1
27
27
return True
28
28
29
+ def palperm2 (string ):
30
+ letter_frequencies = collections .defaultdict (int )
31
+ for letter in string .lower ().replace (' ' , '' ):
32
+ letter_frequencies [letter ] += 1
33
+
34
+ odd_frequency_count = 0
35
+ for frequency in letter_frequencies .values ():
36
+ if frequency % 2 == 0 :
37
+ continue
38
+ odd_frequency_count += 1
39
+ if odd_frequency_count > 1 :
40
+ return False
41
+ return True
29
42
30
43
class Test (unittest .TestCase ):
31
44
def test1 (self ):
@@ -38,5 +51,15 @@ def test1(self):
38
51
self .assertTrue (palperm (input_string3 ))
39
52
self .assertTrue (palperm (input_string4 ))
40
53
54
+ def test2 (self ):
55
+ input_string = "Tact Coa"
56
+ input_string2 = "nick"
57
+ input_string3 = "saippuakivikauppias"
58
+ input_string4 = "iasppaukivikauppias"
59
+ self .assertTrue (palperm2 (input_string ))
60
+ self .assertFalse (palperm2 (input_string2 ))
61
+ self .assertTrue (palperm2 (input_string3 ))
62
+ self .assertTrue (palperm2 (input_string4 ))
63
+
41
64
if __name__ == '__main__' :
42
65
unittest .main ()
You can’t perform that action at this time.
0 commit comments