I'm looking to enumerate all the partitions of n in k parts.
So for p(5,3) i'd get 2 partitions of k = 3 => (3,1,1), (2,2,1).
Here's what I found from searching and looking through stackoverflow :
def p(n,k):
lst = []
if n < k:
return lst
elif k == 1:
return lst
elif k == n:
return lst
else:
p(n-1, k-1)
p(n-k, k)
return lst
^^^^ This is the form i want,
As it is, finding the sum of k parts is easy, you return p(n-1, k-1) + p(n-k,k). As for me, I need to list each element like so [(3,1,1), (2,2,1)].
My main problem is to "build" those partitions recursively. How would you tackle this?
Edit
If you get base case k = 1, add + 1, k-1 times. (4,1) then (4,1,1)
If you get base case k = n, split up and remove one to each part.
Like so : (3,3) then (3,3,3) then (2,2,2)
If you get base case k < n, nothing
Basically, my problem is to "stack" up the ones from base case to top and get a complete list p(6,3) = [(2,2,2), (4,1,1), (3,2,1)]