Skip to content

Instantly share code, notes, and snippets.

@igorvanloo
Last active July 21, 2021 06:35
Show Gist options
  • Select an option

  • Save igorvanloo/f5c877efe5e3dbc95a315da87a2e6e0c to your computer and use it in GitHub Desktop.

Select an option

Save igorvanloo/f5c877efe5e3dbc95a315da87a2e6e0c to your computer and use it in GitHub Desktop.
Problem 76
def Partition(goal, alist):
ways = [1] + [0] * (goal)
for options in alist:
for i in range(len(ways) - options):
ways[i + options] += ways[i]
return ways[-1]-1
'''
Sample Output for partitioning 5:
Current Table [1, 0, 0, 0, 0, 0]
Current number being checked 2
Current i: 0
Current i: 1
Current i: 2
Current i: 3
Current Table [1, 1, 1, 1, 1, 1]
Current number being checked 2
Current i: 0
Current i: 1
Current i: 2
Current i: 3
Current Table [1, 1, 2, 2, 3, 3]
Current number being checked 3
Current i: 0
Current i: 1
Current i: 2
Current Table [1, 1, 2, 3, 4, 5]
Current number being checked 4
Current i: 0
Current i: 1
Current Table [1, 1, 2, 3, 5, 6]
Current number being checked 5
Current i: 0
Total number of ways to partition 5 is 6
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment