about summary refs log tree commit diff
path: root/scratch/deepmind/part_two/permutation-palindrome.py
diff options
context:
space:
mode:
authorWilliam Carroll <wpcarro@gmail.com>2020-02-24T16·24+0000
committerWilliam Carroll <wpcarro@gmail.com>2020-03-01T22·32+0000
commit93d654df779c580e1755e66078975838b6845904 (patch)
tree1058cb1e448d35d180f0a90d3e31e9175f653007 /scratch/deepmind/part_two/permutation-palindrome.py
parent6249e19cdc2a44664e7b06d35ff0af91eca640b9 (diff)
Solve InterviewCake permutation-palindrome problem
Write a predicate to test whether any permutation of an input string is a
palindrome.
Diffstat (limited to 'scratch/deepmind/part_two/permutation-palindrome.py')
-rw-r--r--scratch/deepmind/part_two/permutation-palindrome.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/scratch/deepmind/part_two/permutation-palindrome.py b/scratch/deepmind/part_two/permutation-palindrome.py
new file mode 100644
index 0000000000..730b4bfdc8
--- /dev/null
+++ b/scratch/deepmind/part_two/permutation-palindrome.py
@@ -0,0 +1,37 @@
+import unittest
+from collections import Counter
+
+
+def has_palindrome_permutation(xs):
+    vs = Counter(xs).values()
+    return len([v for v in vs if v % 2 == 1]) in {0, 1}
+
+
+# Tests
+class Test(unittest.TestCase):
+    def test_permutation_with_odd_number_of_chars(self):
+        result = has_palindrome_permutation('aabcbcd')
+        self.assertTrue(result)
+
+    def test_permutation_with_even_number_of_chars(self):
+        result = has_palindrome_permutation('aabccbdd')
+        self.assertTrue(result)
+
+    def test_no_permutation_with_odd_number_of_chars(self):
+        result = has_palindrome_permutation('aabcd')
+        self.assertFalse(result)
+
+    def test_no_permutation_with_even_number_of_chars(self):
+        result = has_palindrome_permutation('aabbcd')
+        self.assertFalse(result)
+
+    def test_empty_string(self):
+        result = has_palindrome_permutation('')
+        self.assertTrue(result)
+
+    def test_one_character_string(self):
+        result = has_palindrome_permutation('a')
+        self.assertTrue(result)
+
+
+unittest.main(verbosity=2)