Last active
April 24, 2021 13:13
-
-
Save jonathanabila/96b12000d92c0abeef886c953c4f1fef to your computer and use it in GitHub Desktop.
Mimic challenge
This file contains 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
from itertools import combinations | |
def permute(k, combo): | |
if k == 1: | |
yield combo | |
else: | |
for hp in permute(k - 1, combo): | |
yield hp | |
for i in range(0, k-1): | |
j = 0 if (k % 2) == 1 else i | |
combo[j], combo[k - 1] = combo[k - 1], combo[j] | |
for hp in permute(k - 1, combo): | |
yield hp | |
def sub_sets(word): | |
return sum(list(list(combinations(word, n)) for n in range(1, len(word)+1)), []) | |
def main(word, permuter, sub_setter): | |
for s in sub_setter(word): | |
for permutation in permuter(len(s), list(s)): | |
print(f" - {''.join(permutation)}") | |
def combos(word): | |
main(word.upper(), permute, sub_sets) | |
if __name__ == '__main__': | |
combos("abc") |
This file contains 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
from unittest import TestCase | |
from unittest.mock import MagicMock | |
from main import sub_sets, permute, main | |
class TestMain(TestCase): | |
def setUp(self) -> None: | |
self.sub_sets = sub_sets | |
self.permute = permute | |
self.main = main | |
def test_sub_sets(self): | |
self.assertEqual(self.sub_sets("AB"), [('A',), ('B',), ('A', 'B')]) | |
def test_permute(self): | |
permutations = [i.copy() for i in self.permute(1, ["A"])] | |
self.assertEqual(permutations, [["A"]]) | |
permutations = [i.copy() for i in self.permute(2, ["A", "B"])] | |
self.assertEqual(permutations, [["A", "B"], ["B", "A"]]) | |
def test_main(self): | |
permuter = MagicMock() | |
sub_setter = MagicMock() | |
sub_setter.return_value = ["A"] | |
permuter.permutations = ["A"] | |
self.assertIsNone(self.main("a", permuter, sub_setter)) | |
sub_setter.called_with("AB") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment