MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / partition

Function partition

dynamic_programming/integer_partition.py:18–29  ·  view source on GitHub ↗
(m)

Source from the content-addressed store, hash-verified

16gives a partition of n-k into k parts. These two facts together are used for this algorithm.
17'''
18def partition(m):
19 memo = [[0 for _ in xrange(m)] for _ in xrange(m+1)]
20 for i in xrange(m+1):
21 memo[i][0] = 1
22
23 for n in xrange(m+1):
24 for k in xrange(1, m):
25 memo[n][k] += memo[n][k-1]
26 if n-k > 0:
27 memo[n][k] += memo[n-k-1][k]
28
29 return memo[m][m-1]
30
31if __name__ == '__main__':
32 import sys

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected