
February 24th, 2013, 01:46 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 1
Time spent in forums: 58 m 30 sec
Reputation Power: 0
|
|
|
Counting block combinations
I am given the following problem : A row measuring seven units in length has red blocks with a minimum length of three units placed on it, such that any two red blocks (which are allowed to be different lengths) are separated by at least one black square. There are exactly seventeen ways of doing this.
How many ways can a row measuring fifty units in length be filled?
Here's what I've wrote so far in python:
Code:
cache = [0]*51
def prob(blocksize,colorsize):
solutions = 1
if(colorsize>blocksize): return solutions
if(cache[blocksize] != 0): return cache[blocksize]
for position in range(0,blocksize-colorsize+1):
#print('\n',"New position:",position)
for blocklength in range(colorsize,blocksize-position+1):
#print("P:",position,"S:",blocklength,"Res:",solutions)
solutions+=prob(blocksize-position-blocklength-1,colorsize)
#print("Blocksize:",blocksize-position-blocklength-1,"Colorsize:",colorsize)
cache[blocksize] = solutions
return solutions
print(prob(50,3))
Can someone please explain to me how this recursive function works please. I've been studying recursive function and I understood how it works.The problem is that I cannot understand how the speffic one works.
|