Skip to content

Instantly share code, notes, and snippets.

@Harduim
Created June 25, 2022 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Harduim/977ef1aaffd6f5a5a287dc19d638a134 to your computer and use it in GitHub Desktop.
Save Harduim/977ef1aaffd6f5a5a287dc19d638a134 to your computer and use it in GitHub Desktop.
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