Created
November 2, 2019 15:33
-
-
Save gayalkuruppu/e54bd306cea630d3c41d80cb5387c054 to your computer and use it in GitHub Desktop.
Number partition in Python
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
def partition(n, c=[], k=1): | |
if n == 0: | |
yield c | |
for i in range(k, n + 1): | |
for p in partition(n - i, c + [i], i): | |
yield p | |
# Example: Partition 10 as a sum of integers | |
for j in partition(10): | |
print ' + '.join(map(str, j)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment