Created
December 11, 2019 15:32
-
-
Save Nastaliss/2bd07b6b88cc257eea58c0096257ed25 to your computer and use it in GitHub Desktop.
Find all unique combinations of a given length for an input of a given size
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
def find_combinations(input_size=3, output_size=3): | |
return find_combinations_in_array( | |
list(range(1, input_size+1)), [], [], output_size) | |
def find_combinations_in_array(remaining_digits, combinations, combination, combination_length): | |
print('IN', remaining_digits, combination, combination, combination_length) | |
# exit strategy | |
# If the combination is already *combination_length* long | |
if len(combination) == combination_length: | |
print('LEN OK') | |
combinations = [ | |
*combinations, combination] if combination not in combinations else combinations | |
combination = [] | |
return combinations | |
for (index, number) in enumerate(remaining_digits): | |
combinations = find_combinations_in_array( | |
remaining_digits[index+1:], combinations, [*combination, number], combination_length) | |
return combinations | |
print('OUT') | |
find_combinations() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment