Skip to content

Instantly share code, notes, and snippets.

@skovorodkin
Last active August 25, 2017 14:20
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 skovorodkin/3f6678d64397523cd1b05779b666d726 to your computer and use it in GitHub Desktop.
Save skovorodkin/3f6678d64397523cd1b05779b666d726 to your computer and use it in GitHub Desktop.
Partitions with permutations
# https://stackoverflow.com/questions/10035752/elegant-python-code-for-integer-partitioning/44209393?#comment78597595_44209393
from itertools import chain, permutations
def partitions(n, I=1):
yield (n,)
for i in range(I, n//2 + 1):
for p in partitions(n-i, i):
yield (i,) + p
def partitions_with_permutations(n):
return chain.from_iterable(set(permutations(p)) for p in partitions(n))
n = 5
print(list(partitions_with_permutations(n)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment