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

Ok, guys, i am working on PS2 and here is what i have written so far: def HowManyNuggets(nuggets): counter = 0 for a in range(1,100): for b in range(1,100): for c in range(1,100): if 6*a + 9*b + 20*c == nuggets: counter += 1 print "Solution number", counter, "is", a, b, c, "=", nuggets This function takes the number of nuggets as an argument and returns possible combinations of nuggets packs. So, for example, HowManyNuggets(50)will return "2 2 1 = 50" Am i missing anything?

OpenStudy (anonymous):

your code may not work on bigger number of nuggets ..so try to find a practical solution for the upper range u choose in the for loops.. and another thingwhat is the counter doing?

OpenStudy (anonymous):

Oops, counter actually is an artefact and doing nothing. So, you tyhink the code is gonna be too slow?

OpenStudy (anonymous):

Oh, i am sorry. counter is counting the amount of possible solutions for the expression: Solution number 1 is 1 3 1 = 53 Solution number 2 is 4 1 1 = 53

OpenStudy (anonymous):

Hm... i think i can create a list or a tuple of three possible amounts of nuggets and loop through them, instead of looping from 1 to 100. Thanks for pointing into a right direction, Sunu. The word "practical" was the key! :)

OpenStudy (anonymous):

another thing...try a input greater than 3500..u will see what i mean

OpenStudy (anonymous):

Counting the amount of possible solutions? Wow, you do like to complicate things, do you? Just convert your for loops to while. I think you can implement some kind of input (maybe put the whole thing in a function and pass in a parameter), and use that input to calculate the upper bound for your loops.

OpenStudy (anonymous):

Nvm, I see it's already in a function.

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!