Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 17 Online
OpenStudy (anonymous):

about ps2a: I have solved problem 3 in the following manner: http://pastebin.com/cFp6tdKa But I can't figure out how to make it stop when I reach 6 consecutive divisibles (like suggested on Problem Set 2 Guidelines). I've seen solutions that pull this off (e.g: http://code.google.com/p/intro-cs-dw/source/browse/elz/ps2a.py?r=33a88e243bfc2926afc1024a4d8705a0acfedd03) but I want to do it without using functions since that tool belongs to lesson 4. Is it possible to do it without functions?

OpenStudy (anonymous):

Ok, cool code / good job so far but there are a few things to clean up. Some quick comments: (1) Re: your for loops: remember, you are checking every possible integer triple (x,y,z) for a solution to 6x + 9y + 20z == n. In your code for, say, n = 6 for the first set of triples it tests (0,0,0) we get 6*0 + 9*0 + 20*0 == 0, which definitely doesn't equal 6. So your code assigns 6 to Impossible, but then also assignes it to Possible when it tests (1,0,0). I see how you work around this with the additional for loops through Impossible and Possible at the end, but you are doing a lot of computation here that you don't need to. Once you've found a solution for 6x + 9y + 20z == n, you can break out of the loops entirely - see the modified code I've written to understand how to do this http://pastebin.com/81T00cDW - Doing this also eliminates the need to check through Possible and Impossible as you do at the end, and it also eliminates the need for the Indivisible var altogether. All in all, this will cut down on your run time significantly. (2) So, finally, re: your question - you need a counter variable at this section of the code, once you've broken out of the for loops, for counting consecutive solutions. So, within an 'if' statement, you'll need a statement with the ol' "+=" trick to add to this counter and then put an additional statement somewhere in your code that checks if this counter gets to 6 and stops once doing so. Hope this helps / lemme know if you have any other questions!

OpenStudy (anonymous):

You can check out how I solved this problem here: http://pastebin.com/ACApbS9J It is very similar to the way you did it - with loops inside of loops. I used a comparison between my "successful_n" variable and "bestSoFar" which was tracking the failed n values. Once they were more then 6 apart, I had 6 "successful_n" values in a row, and ended the loop. I'm not sure if this would work in your code, but it might be worth trying out.

OpenStudy (anonymous):

Awesome. Here's my solution without functions (and computing until successive < 6): http://pastebin.com/yVDfn39h One more thing: how could I replace the end value of each range (a,b,c) for variables max a, max b, max c so I can save a few more computations. I tried to do it but I get timeout. Just to see what I have in mind: http://pastebin.com/jAHNmg3m (I know it doesn't work like this but it's a way to visualize what I would like to happen with those ranges).

OpenStudy (anonymous):

i broke out of the loop if it was getting too big http://dpaste.com/709721/

OpenStudy (anonymous):

Great revisions - to avoid the timeout (I think - I haven't tried your code specifically) - try adding a "+1" to the end of your maxa etc. vars. Since the division rounds down, you aren't necessarily checking all possible solutions the way you have the vars set up now.

OpenStudy (anonymous):

Even like this, I don't get any output. Thanks anyway.

OpenStudy (anonymous):

Ha yeah fair enough - sorry I didn't look closely enough, time makes me anxious, but I tweaked your code a little and here's the solution. http://pastebin.com/WHeugzPN - minor thing: you didn't have your if clauses that were breaking out of the loops indented correctly but whatever. The main problem is look at the ranges of your for loops. You are ranging from 0 to n/20 for the first one. But n=1. So you are ranging from 0 to 0, b/c that pellet rounds down u know. That's why ur program breakin'. Keep this in mind for part b of the problem set :) gl

OpenStudy (anonymous):

hmm, looks like ol'MIT is censoring me. by "pellet" i mean "sh it" without that middle space

OpenStudy (anonymous):

Thanks again! I've corrected indentation. Here's the ps2b: I think it's working ok: http://codepad.org/aooQxMDd

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!