Created
June 25, 2022 15:31
-
-
Save Harduim/977ef1aaffd6f5a5a287dc19d638a134 to your computer and use it in GitHub Desktop.
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 diminishing_combinations(arr: list): | |
for i in range(len(arr)): | |
for c in combinations(arr, i): | |
if len(c) < 1: | |
continue | |
yield c | |
yield tuple(arr) | |
# Test cases | |
assert list(diminishing_combinations([])) == [()] | |
assert list(diminishing_combinations([10])) == [(10,)] | |
assert list(diminishing_combinations([10, 20])) == [(10,), (20,), (10, 20)] | |
assert list(diminishing_combinations([10, 20, 30])) == [ | |
(10,), | |
(20,), | |
(30,), | |
(10, 20), | |
(10, 30), | |
(20, 30), | |
(10, 20, 30), | |
] | |
assert list(diminishing_combinations([10, 20, 30, 40])) == [ | |
(10,), | |
(20,), | |
(30,), | |
(40,), | |
(10, 20), | |
(10, 30), | |
(10, 40), | |
(20, 30), | |
(20, 40), | |
(30, 40), | |
(10, 20, 30), | |
(10, 20, 40), | |
(10, 30, 40), | |
(20, 30, 40), | |
(10, 20, 30, 40), | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment