Created
March 19, 2021 11:19
-
-
Save ffosilva/80ccf8dd0b44ce49b6b05a6efe3a2ea0 to your computer and use it in GitHub Desktop.
Generate combinations from groups of numbers
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
exemplo = [ | |
[1, 2, 3], | |
[1, 3, 4, 5], | |
[6, 8, 9, 10], | |
[12, 13, 15], | |
[17], | |
[23, 24] | |
] | |
def __next(grupos, pos): | |
for i in range(len(pos) -1, -1, -1): | |
pos[i] += 1 | |
if pos[i] == len(grupos[i]): | |
pos[i] = 0 | |
else: | |
break | |
restarted = True | |
for i in pos: | |
restarted = restarted and i == 0 | |
return restarted | |
def gerador(grp): | |
for grupo in grp: | |
assert(type(grupo) == list) | |
# cria cópia e ordena todos os grupos | |
grupos = list(map(list, list(map(set, grp.copy())))) | |
for e in grupos: | |
e.sort() | |
num_grupos = len(grupos) | |
iter_grupos = [0 for _ in range(num_grupos)] | |
while True: | |
print(iter_grupos) | |
if __next(grupos, iter_grupos): | |
break | |
if __name__ == "__main__": | |
gerador(exemplo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment