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
# Python does not have tail recursion optimization, | |
# so this function can't handle too deep data structures. | |
# This piece of code was written as a practicing task. | |
def flatten(l): | |
nested = None | |
for i, e in enumerate(l): | |
if isinstance(e, list): | |
nested = e; break | |
if nested is not None: |
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
from itertools import product | |
def combinations(d): | |
""" | |
>>> d = {'a': [2, 5, 6], 'b': [8, 3, 6]} | |
>>> combinations(d) | |
[{'a': 2, 'b': 8}, {'a': 2, 'b': 3}, {'a': 2, 'b': 6}, {'a': 5, 'b': 8}, {'a': 5, 'b': 3}, {'a': 5, 'b': 6}, {'a': 6, 'b': 8}, {'a': 6, 'b': 3, {'a': 6, 'b': 6}] | |
""" | |
return [dict(combo) for combo in product(*[ |