Skip to content

Commit ab0afe4

Browse files
palperm2
1 parent 898d4a0 commit ab0afe4

File tree

1 file changed

+24
-1
lines changed
  • Python/chapter01/1.4 - PalinPerm

1 file changed

+24
-1
lines changed

Python/chapter01/1.4 - PalinPerm/Nick4.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'''
77

88
import unittest
9-
9+
import collections
1010

1111
def palperm(string):
1212
'''
@@ -26,6 +26,19 @@ def palperm(string):
2626
count += 1
2727
return True
2828

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
2942

3043
class Test(unittest.TestCase):
3144
def test1(self):
@@ -38,5 +51,15 @@ def test1(self):
3851
self.assertTrue(palperm(input_string3))
3952
self.assertTrue(palperm(input_string4))
4053

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+
4164
if __name__ == '__main__':
4265
unittest.main()

0 commit comments

Comments
 (0)