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

Hi all Please help me to find the error in returnig the function def prob(): n=int(raw_input('Please Enter the Highest value to check')) if (n<6): print'Please enter the value more than 5' prob() else: m=SolDio(n) print m def SolDio(a): set_sol=False for box1 in range(0,a): for box2 in range(0,a-box1): for box3 in range(0,a-box1-box2): nuggets=6*box1+box2*9+box3*20 if(nuggets==a): set_sol=True SolDio(a-1) if not set_sol:

OpenStudy (anonymous):

First of all, what is the error that is being returned? It would be really helpful if you could put in comments to explain what you are doing, what values your variables refer to, etc... it's really hard for me (granted I'm a beginner) to just look at bare code and know what each step is attempting to do, and what the overall goal of the program is.

OpenStudy (anonymous):

OK first things first, you need some command after that last if statement or it gets an error message. Fixed that and added some print statements to tell me what equation it's running etc. (side note: this is a really useful tool for me when I don't know what's going on with my code). I tested SolDio at 50. The first thing I notice is that there are a LOT of extra steps. The program tests from 0 boxes all the way to 50 for each set of boxes (well technically, to 50, 49 and 48 respectively). But this is unnecessary as the maximum possible number of large boxes, for example, would be 2, because any greater value would return 60. In general, you can reign in the number of boxes to be less than or equal to a/(number of nuggets in box). However, for any large number of nuggets, this will still create a massive number of permutations and so you should probably look for another way to go through the number of possible solutions. The other thing I'd add is that I don't really see the point of the set_sol = True line. First of all, I'm not sure of it's purpose (hearkening back to my earlier point about clarity) and second of all, any path that switches this variable to True will also immediately reset it to False because the function will repeat, so it's an irrelevant action. I'm not sure if I addressed anything pertinent to you but I hope so.

OpenStudy (anonymous):

Hi ThinkPad, Thanks for checking this..I am trying to solve the third problem of Problem Set 2. ie.to find the "“Largest number of McNuggets that cannot be bought in exact quantity: <n>”" Equation 6x+9y+20z=n..Where n can be any value..My program working find till finding the value but it is unable to return the value at the last step Here the program again with Comments.. def prob(): '''This program is written to find the Highest non Solution value of Equatioin 6x+9y+20z=N in the region(0,N)''' n=int(raw_input('Please Enter the Highest value to check')) if (n<6): #Condition to check if N is more than 5 to get a solution for the Equation# print'Please enter the value more than 5' prob() else: m=SolDio(n) #Calling function SolDio print m def SolDio(a): set_sol=False for box1 in range(0,a): #For loop for Value x for box2 in range(0,a-box1): #For Loop for Value y for box3 in range(0,a-box1-box2): #For Loop for Value z nuggets=6*box1+box2*9+box3*20 #checking if the x,y,z values create solution if(nuggets==a): set_sol=True SolDio(a-1) #Calling the same fuction to check for next Decrement level if not set_sol: #if Solution is not found then return that value return a

OpenStudy (anonymous):

I did the same problem set a little while ago but I did it a little differently. What you want to find is the highest number that cannot be solved exactly with the boxes you have. Here's what I did. First I wrote a program that found a solution for any given target. I did something very similar to yours, but mine stopped as soon as it found a solution, which makes it run a lot faster because it's not going through every permutation. You can do away with the true/false test and instead simply have it return an answer as soon as it finds one. If it doesn't find any, have it return "No solutions" or whatever. After you have that function working correctly, you just have to make a program that tests a lot of values. Here's what I had: def findthenonsolutions(upperbound): nonsolutions = () for n in range(0,upperbound+1): if nuggetguide(n) == "Nosol": nonsolutions += (n,) else: nonsolutions += ("Solved",) return nonsolutions My SolDio was called nuggetguide and it returned "Nosol" if it didn't find a solution. So, this program tests every N up to a limit of my choosing and, if that n returns "Nosol", it adds it to the list. Then it returns the list. Having the upper bound is helpful because you don't have to worry about any iterations spiraling out of control. So, in summary I solved this with two functions; one that found a solution for a given N, and another that tested a bunch of Ns in a given range. That might not be the only way to do it, but it worked for me, in particular because the original function (nuggetguide) can be easily modified to help with the other problems.

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!