Partition Problem
Donovan Hide
donovanhide at bigfoot.com
Sun Jul 15 20:07:35 EDT 2001
Hi,
thanks for the responses, I can see the ambiguity in the original
question. What I am actually looking for is the enumeration of all the 6
whole numbers greater than 0 that sum to 17.
After that, another enumeration is required, this time to find all 6
whole numbers greater than 0 that sum to 20. Order is unimportant.
I have found a book that covers this exact area, Combinatorial
Algorithms by Kreher and Stinson. Their source for the book is at
http://www.math.mtu.edu/~kreher/cages/Src.html
They have used recursive algorithms to solve this problem and many other
similar partitioning problems I had no idea that existed. However, it is all
coded in C!
Also working in this area is Frank Ruskey,
http://www.csr.uvic.ca/~fruskey/, who seems to be the main man.
So, apart from this short bibliography, I am still stuck. I don't want
any of the readers of this newsgroup to do my homework, because this isn't
homework. I am learning Python casually and this problem seemed like a good
starting point for a newbie.
Waffle aside,
can someone fill the gaps:
def Partition(n,k):
res=[[0]*k] #results list, each result
nested in the main list
res[0][0]=(n-k+1) #enumerate first result
for x in range(1,k):
res[0][x]=1
step=1
while 1: #iterate through remaining
possible partitions
next=NextPartition(n,k,step)
step=step+1
if next !=0:
res.append(next)
else:
break
return res
def NextPartition(n,k,step):
#generate next possible partition
#return next possible partition as list
#if no more partitions left
return 0
a=Partition(7,4)
print a
[[4, 1, 1, 1], [3, 2, 1, 1],[2,2,2,1]]
Cheers,
Donovan Hide.
P.S. A pint for the winning Algorithm Master!
More information about the Python-list
mailing list