Skip to content

Instantly share code, notes, and snippets.

@meeuw
Created March 14, 2015 19:44
Show Gist options
  • Select an option

  • Save meeuw/b9f687a55a7bb371f4bd to your computer and use it in GitHub Desktop.

Select an option

Save meeuw/b9f687a55a7bb371f4bd to your computer and use it in GitHub Desktop.
generate all combinations to a specified sum
def recurse(lst, c):
s = sum(lst)
if s > c:
return
if s == c:
print lst
return
for i in range(1, c+1):
if lst and lst[-1] < i: continue
recurse(lst+[i], c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment