fork download
  1. def permutations(items):
  2. if len(items) == 1:
  3. yield tuple(items)
  4. return
  5. for index, fixed in enumerate(items):
  6. without_fixed = items[:index] + items[index + 1:]
  7. for x in permutations(without_fixed):
  8. yield (fixed, *x)
  9.  
  10.  
  11. for p in permutations(list('abcd')):
  12. print(''.join(p))
Success #stdin #stdout 0.02s 9064KB
stdin
Standard input is empty
stdout
abcd
abdc
acbd
acdb
adbc
adcb
bacd
badc
bcad
bcda
bdac
bdca
cabd
cadb
cbad
cbda
cdab
cdba
dabc
dacb
dbac
dbca
dcab
dcba