Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 24th, 2013, 01:46 PM
S2xCracker S2xCracker is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 1 S2xCracker User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old February 24th, 2013, 03:29 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is online now
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,350 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 7 h 38 m 45 sec
Reputation Power: 383
Project Euler #114.

I haven't yet solved #114. You've made interesting comments: "The program you wrote that you don't understand."


The program would be easier for me to read if the variable names corresponded to the problem description, block and row. I could refactor to please myself...


These conditions terminate the recursion. The second says "if this is already solved use the cached answer".

if(colorsize>blocksize): return solutions
if(cache[blocksize] != 0): return cache[blocksize]



for position in range(0,blocksize-colorsize+1):

describes all the places a block could be inserted (by "left edge")



for blocklength in range(colorsize,blocksize-position+1):

are the lengths of red blocks that would fit.



solutions+=prob(blocksize-position-blocklength-1,colorsize)

Note that the recursion depends on both iteration variables, position and blocklength. In other words, the number of solutions is the number of ways to get populate to here plus the number of ways to populate the remaining of the space after sticking in said red block at said position.



I hope this didn't help.
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Counting block combinations

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap