Created
June 30, 2018 03:01
-
-
Save birkholz/1fe91c296df2069cfd0a6e2d9b67fe9e to your computer and use it in GitHub Desktop.
Permutations of a String
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
# Code | |
def get_permutations(input_str): | |
input_len = len(input_str) | |
results = [] | |
if input_len == 0: | |
results.append('') | |
elif input_len == 1: | |
# Return the original | |
results.append(input_str) | |
else: | |
for index in range(input_len): | |
# For each character, add to results the character followed by the permutations of the rest | |
input_copy = list(input_str) | |
char = input_copy[index] | |
del input_copy[index] | |
sub_permutations = get_permutations(''.join(input_copy)) | |
results += [f'{char}{permutation}' for permutation in sub_permutations] | |
return results | |
# Tests | |
class PermutationTests(unittest.TestCase): | |
def test_empty_str(self): | |
results = get_permutations('') | |
expected = [''] | |
self.assertEqual(results, expected) | |
def test_one_char(self): | |
results = get_permutations('a') | |
expected = ['a'] | |
self.assertEqual(results, expected) | |
def test_two_chars(self): | |
results = get_permutations('ab') | |
expected = ['ab', 'ba'] | |
self.assertListEqual(results, expected) | |
def test_three_chars(self): | |
results = get_permutations('abc') | |
expected = ['abc', 'acb', 'bac', 'bca', 'cab', 'cba'] | |
self.assertListEqual(results, expected) | |
def test_four_chars(self): | |
results = get_permutations('abcd') | |
expected = [ | |
'abcd', 'abdc', 'acbd', 'acdb', 'adbc', 'adcb', | |
'bacd', 'badc', 'bcad', 'bcda', 'bdac', 'bdca', | |
'cabd', 'cadb', 'cbad', 'cbda', 'cdab', 'cdba', | |
'dabc', 'dacb', 'dbac', 'dbca', 'dcab', 'dcba' | |
] | |
# For each additional character, the results include every permutation of the remaining characters following | |
self.assertListEqual(results, expected) | |
def test_five_chars(self): | |
results = get_permutations('abcde') | |
expected = [ | |
'abcde', 'abced', 'abdce', 'abdec', 'abecd', 'abedc', 'acbde', 'acbed', 'acdbe', 'acdeb', 'acebd', 'acedb', | |
'adbce', 'adbec', 'adcbe', 'adceb', 'adebc', 'adecb', 'aebcd', 'aebdc', 'aecbd', 'aecdb', 'aedbc', 'aedcb', | |
'bacde', 'baced', 'badce', 'badec', 'baecd', 'baedc', 'bcade', 'bcaed', 'bcdae', 'bcdea', 'bcead', 'bceda', | |
'bdace', 'bdaec', 'bdcae', 'bdcea', 'bdeac', 'bdeca', 'beacd', 'beadc', 'becad', 'becda', 'bedac', 'bedca', | |
'cabde', 'cabed', 'cadbe', 'cadeb', 'caebd', 'caedb', 'cbade', 'cbaed', 'cbdae', 'cbdea', 'cbead', 'cbeda', | |
'cdabe', 'cdaeb', 'cdbae', 'cdbea', 'cdeab', 'cdeba', 'ceabd', 'ceadb', 'cebad', 'cebda', 'cedab', 'cedba', | |
'dabce', 'dabec', 'dacbe', 'daceb', 'daebc', 'daecb', 'dbace', 'dbaec', 'dbcae', 'dbcea', 'dbeac', 'dbeca', | |
'dcabe', 'dcaeb', 'dcbae', 'dcbea', 'dceab', 'dceba', 'deabc', 'deacb', 'debac', 'debca', 'decab', 'decba', | |
'eabcd', 'eabdc', 'eacbd', 'eacdb', 'eadbc', 'eadcb', 'ebacd', 'ebadc', 'ebcad', 'ebcda', 'ebdac', 'ebdca', | |
'ecabd', 'ecadb', 'ecbad', 'ecbda', 'ecdab', 'ecdba', 'edabc', 'edacb', 'edbac', 'edbca', 'edcab', 'edcba' | |
] | |
self.assertListEqual(results, expected) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment