Created
February 23, 2023 16:00
-
-
Save filwaline/70b1c5fa1d9d2c20224849f3544dcbc6 to your computer and use it in GitHub Desktop.
full comb
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 _comb_1(last_subsets, elem, head, rest, full_result, current_subsets): | |
new_subset = list(sorted(set(elem + head))) | |
if new_subset not in full_result: | |
full_result = full_result + [new_subset] | |
current_subsets = current_subsets + [new_subset] | |
if rest: | |
return _comb_1( | |
last_subsets, | |
elem, | |
rest[0], | |
rest[1:], | |
full_result, | |
current_subsets, | |
) | |
return full_result, current_subsets | |
def _comb_2(full_result, last_subsets, current_subsets, i, array): | |
elem = [array[i]] | |
full_result, current_subsets = _comb_1( | |
last_subsets, | |
elem, | |
last_subsets[0], | |
last_subsets[1:], | |
full_result, | |
current_subsets, | |
) | |
if len(full_result) == 2 ** len(array): | |
return full_result | |
if i == len(array) - 1: | |
return _comb_2(full_result, current_subsets, [], 0, array) | |
return _comb_2( | |
full_result, last_subsets, current_subsets, (i + 1) % len(array), array | |
) | |
def full_comb(array): | |
return _comb_2([[]], [[]], [], 0, array) | |
print(full_comb([3, 4, 5, 10])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment