I am trying to create a list of all possible combinations of numbers in a set of four numbers, using all numbers from 0 to 9.
I’m getting closer, but the output does not display all possible combinations, starting from 0000 to 9999.
Any hints as to why the following code discards certain combinations?
def permgen (items, n):
if n == 0: yield []
else:
for i in range (len (items)):
for cc in permgen (items [: i] + items [i + 1:], n-1):
yield [items [i]] + cc
if __name __ == "__ main__":
for c in permgen (['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], 4): print '' .join (c)
alan
source
share