Skip to content

Instantly share code, notes, and snippets.

@mitchellhuang
Created November 21, 2014 02:01
Show Gist options
  • Save mitchellhuang/ecb25d03f3be18ad2535 to your computer and use it in GitHub Desktop.
Save mitchellhuang/ecb25d03f3be18ad2535 to your computer and use it in GitHub Desktop.
proj4 q19 doeeeeee
def partitions(n, m):
"""Return a linked list of partitions
of n using parts of up to m.
Each partition is a linked list.
"""
if n == 0:
return link(empty, empty)
elif n < 0:
return empty
elif m == 0:
return empty
else:
# Do I use at least one m?
yes = partitions(n-m, m)
no = partitions(n, m-1)
add_m = lambda s: link(m, s)
yes = apply_to_all_link(add_m, yes)
return extend(yes, no)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment