diff options
author | William Carroll <wpcarro@gmail.com> | 2020-02-12T16·58+0000 |
---|---|---|
committer | William Carroll <wpcarro@gmail.com> | 2020-02-12T16·58+0000 |
commit | fabf1c9334a86d55be133da851cafccc9e6319fe (patch) | |
tree | e5c07cbe676c4955d9a48742d00e0081b16b3446 /scratch/data_structures_and_algorithms/permutation-palindrome.py | |
parent | 5ec5a6da8cbfe3c35558fd2c17ef779b5d0ccb54 (diff) |
Tidy up structure of briefcase
I had a spare fifteen minutes and decided that I should tidy up my monorepo. The work of tidying up is not finished; this is a small step in the right direction. TL;DR - Created a tools directory - Created a scratch directory (see README.md for more information) - Added README.md to third_party - Renamed delete_dotfile_symlinks -> symlinkManager - Packaged symlinkManager as an executable symlink-mgr using buildGo
Diffstat (limited to 'scratch/data_structures_and_algorithms/permutation-palindrome.py')
-rw-r--r-- | scratch/data_structures_and_algorithms/permutation-palindrome.py | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/scratch/data_structures_and_algorithms/permutation-palindrome.py b/scratch/data_structures_and_algorithms/permutation-palindrome.py new file mode 100644 index 000000000000..0a2136a408f2 --- /dev/null +++ b/scratch/data_structures_and_algorithms/permutation-palindrome.py @@ -0,0 +1,49 @@ +from collections import Counter +import unittest + + +################################################################################ +# Impl +################################################################################ +# palindromifiable :: String -> Boolean +def has_palindrome_permutation(x): + bag = Counter(x) + odd_entries_ct = 0 + + for _, y in bag.items(): + if y % 2 != 0: + odd_entries_ct += 1 + + return odd_entries_ct 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) |