This notebook was prepared by Donne Martin. Source and license info is on GitHub.
* None -> None * '' -> '' * 'AABC' -> ['AABC', 'AACB', 'ABAC', 'ABCA', 'ACAB', 'ACBA', 'BAAC', 'BACA', 'BCAA', 'CAAB', 'CABA', 'CBAA']
Complexity:
from collections import OrderedDict
class Permutations(object):
def _build_counts_map(self, string):
counts_map = OrderedDict()
for char in string:
if char in counts_map:
counts_map[char] += 1
else:
counts_map[char] = 1
return counts_map
def find_permutations(self, string):
if string is None or string == '':
return string
counts_map = self._build_counts_map(string)
curr_results = []
results = []
self._find_permutations(counts_map, curr_results, results, len(string))
return results
def _find_permutations(self, counts_map, curr_result,
results, input_length):
for char in counts_map:
if counts_map[char] == 0:
continue
curr_result.append(char)
counts_map[char] -= 1
if len(curr_result) == input_length:
results.append(''.join(curr_result))
else:
self._find_permutations(counts_map, curr_result,
results, input_length)
counts_map[char] += 1
curr_result.pop()
%%writefile test_permutations.py
import unittest
class TestPermutations(unittest.TestCase):
def test_permutations(self):
permutations = Permutations()
self.assertEqual(permutations.find_permutations(None), None)
self.assertEqual(permutations.find_permutations(''), '')
string = 'AABC'
expected = [
'AABC', 'AACB', 'ABAC', 'ABCA',
'ACAB', 'ACBA', 'BAAC', 'BACA',
'BCAA', 'CAAB', 'CABA', 'CBAA'
]
self.assertEqual(permutations.find_permutations(string), expected)
print('Success: test_permutations')
def main():
test = TestPermutations()
test.test_permutations()
if __name__ == '__main__':
main()
Overwriting test_permutations.py
%run -i test_permutations.py
Success: test_permutations