On problem set 1, the diophantine/McNuggies exercise...the 2nd problem... I can find the solutions to (a,b,c) that satisfy 6a+9b+20c=n. I'm stuck on how to find the last n that doesn't have an answer. I put a counter in to add up each time the tuple(a,b,c) = n, but that didn't work. How did y'all do it?
counter =0 six_pack=6 nine_pack=9 twenty=20 mcnuggies = 0 n=1 while (n<60): print ('n= ',n) def mcnuggies(x,y,z): xx=6*x+9*y+20*z return xx for a in range (0,6): for b in range (0,6): for c in range (0,6): if mcnuggies(c,b,a)==n: counter+=1 print (mcnuggies(c,b,a), counter) n=n+1 print ('the most you cant buy is ' ,counter
I meant the 3rd problem....
idk what the problem is, can you post the question here, maybe (just maybe :P) i can help :)
and you're defining a function each time the loop runs? that's almost a crime in programming lol. i wouldn't do that, and i'm trying to understand why you define the function each time? you can define it and call it each time in the loop... or? imho
Write an iterative program that finds the largest number of McNuggets that cannot be bought in exact quantity. Your program should print the answer in the following format (where the correct number is provided in place of <n>): “Largest number of McNuggets that cannot be bought in exact quantity: <n>” Hint: your program should follow the outline above. Hint: think about what information you need to keep track of as you loop through possible ways of buying exactly n McNuggets. This will guide you in deciding what state variables you will need to utilize.
Thanks for pointing that out. While not germane to the problem at hand, it is representative of why I'm here.....
is that the full question or there's another given preamble?
Hypothesize possible instances of numbers of McNuggets that cannot be purchased exactly, starting with 1 For each possible instance, called n, Test if there exists non-negative integers a, b, and c, such that 6a+9b+20c = n. (This can be done by looking at all feasible combinations of a, b, and c) If not, n cannot be bought in exact quantity, save n When you have found six consecutive values of n that in fact pass the test of having an exact solution, the last answer that was saved (not the last value of n that had a solution) is the correct answer, since you know by the theorem that any amount larger can also be bought in exact quantity
that is the preamble for the question
so here's what i'm thinking before i put anything to code and try to answer this question is for me to save 2 things. first the non-negative integers that exist such that 6a+9b+20c = n, and second, the last value of n that had a solution. once i've found six consecutive values of n, those non-negative integers become what i want... am i on track?
where those non-negative integers which become what i want are the last non-negative integers that give me my sixth n value...
do you agree with my logical reasoning of what the question means? i'm trying to understand so... help me (help you)
Once I've found six consecutive solutions, what is the last number that wasn't a solution. That's the question as I understand it.
then the answer should be obvious? i believe... the first number of the consecutive numbers minus 1...
if 11,12,13,14,15, and 16 can be bought in exact quantity, then the last number that wasn't a solution logically should be 10...
i'm trying to solve this but my approach is to save the consecutive numbers in a list, and each time i'm checking my combinations, i also check if the numbers in the list are consecutive, if not, scrap the list back to [] and continue the search. i.e, if i get [23,24,25,26] and 25 cannot be bought in exact quantity, return the list back to [] and continue from 26...
how do I code a list? remember, this class is for programming newbies, and I'm definitely that.
there are types of lists, an array list should be fine, which i'm attempting to use t solve the problem, and you can simply define one like this in python any_name_you_want = [] cats=[] names=[] integers=[] etc
you can search google to read about python array lists for more info
my code appears to be working but i want to run a few more tests before placing my code here to explain. just a few more minutes :)
#we start checking for consecutive numbers that #should pass the test from 1 so n=1 n=1 #we need to store our answer which is the list of consecutive #numbers somewhere so we save it in an array called answer below answer=[] #The question also said that when we find six consecutive numbers #that pass the test, that's our answer so we have to keep checking #if the size of our answer list is up to 6, just to know when we've arrived #at the answer we want while len(answer)<6: for a in xrange(0,9): #this is just a random range, no special reason if len(answer)==6: break for b in xrange(0,9): if len(answer)==6: break for c in xrange(0,9): if ((6*a)+(9*b)+(20*c)==n): #test n, and if it passes if n not in answer: #but is not in already in the answer list, answer.append(n) #add it to the answer list #this is where we check if our list of answers are consecutive numbers... if len(answer)>1: for i in xrange(0,len(answer)): try: if answer[i]!=answer[i+1]-1: answer=[] except IndexError: pass print "answer =", answer if len(answer)==6: break n+=1 print "Consecutive numbers of McNuggets that can be bought in exact quantity: %s" % answer print "Largest num...t cannot be bought in exact quantity: %s" % str(answer[0]-1)
Join our real-time social learning platform and learn together with your friends!