Skip to content

Instantly share code, notes, and snippets.

@Scinawa
Created March 29, 2016 22:53
Show Gist options
  • Save Scinawa/1350a46b7ec7132368cdc6dc4fd871f9 to your computer and use it in GitHub Desktop.
Save Scinawa/1350a46b7ec7132368cdc6dc4fd871f9 to your computer and use it in GitHub Desktop.
Integer Partition
# http://stackoverflow.com/questions/10244180/python-generating-integer-partitions (checked)
def accelAsc(n):
a = [0 for i in range(n + 1)]
k = 1
a[0] = 0
y = n - 1
while k != 0:
x = a[k - 1] + 1
k -= 1
while 2*x <= y:
a[k] = x
y -= x
k += 1
l = k + 1
while x <= y:
a[k] = x
a[l] = y
yield a[:k + 2]
x += 1
y -= 1
a[k] = x + y
y = x + y - 1
yield a[:k + 1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment