Last active
August 25, 2017 14:20
-
-
Save skovorodkin/3f6678d64397523cd1b05779b666d726 to your computer and use it in GitHub Desktop.
Partitions with permutations
This file contains 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
# 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