Skip to content

Commit 856fff5

Browse files
committed
Create palindrome_permutation.py
1 parent 2cdb306 commit 856fff5

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

palindrome_permutation.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import unittest
2+
3+
4+
def is_pal_perm(string):
5+
"""
6+
Takes a string and returns true if the string is a permutation of a
7+
palindrome.
8+
"""
9+
10+
let_counts = {}
11+
odd = 0
12+
13+
for let in string:
14+
let_counts[let] = let_counts.get(let, 0) + 1
15+
16+
for val in let_counts.values():
17+
if val % 2 != 0:
18+
odd += 1
19+
20+
return odd <= 1
21+
22+
23+
class Testing(unittest.TestCase):
24+
25+
def test_is_pal_perm(self):
26+
self.assertTrue(is_pal_perm('carereca'))
27+
self.assertTrue(is_pal_perm('a'))
28+
self.assertFalse(is_pal_perm('carelnreca'))
29+
30+
31+
if __name__ == '__main__':
32+
unittest.main()

0 commit comments

Comments
 (0)