This is my first question - I have worked on problemset 2 - and would like to discuss details - someone interested ;-)
Any topic you have in mind? I completed that pset a long time ago, but maybe I can remember something :-)
OK - great - I made a function to find all the combinations of you can make of the "Nug" packages - and then I just multiplied the combinations with the rigt package size - make sense?
Looks like this: def combinations(n,b=(6,9,20)): k = n/b[2]+1 Nuggets = [] if k == 0: k += 1 for first in range(k): d = first j = ((n-d*b[2])/b[1])+1 ##Making index for b vary on value of a - so number don't get to big for second in range(j): e = second i = ((n-(e*b[1]+d*b[2]))/b[0])+1 ##Making index for c vary on value of b - so that number don't get to big for third in range(i): number = third*b[0] + e*b[1] + d*b[2] if number not in Nuggets: Nuggets.append(number) return Nuggets
But man - so many lines for such a small problem - any ideas ?
and this is how I use combinations: def largestNug(a=200,b=(6,9,20)): Nug = combinations(a,b) ##Making one "pool" of posible Nug numbers - once for all - saving iterations" for i in range(1,a): ##Testing numberes between 1 and a (200) if i not in Nug: n = i counter = 0 else: counter += 1 if counter > 5: print 'Given package sizes ' + str(b[0]) + ', ' + \ str(b[1]) + ' and ' + str(b[2]) + ', the largest ' +\ 'number of McNuggets that cannot be bought ' + \ 'in exact quantity is: ' + str(n) ##Strings are not niece in Python break
First thing: Kudos for the solution :-) That's generally how it's solved (The original idea, I meant). There are many smarter ways, especially using Python that is so great at writing fewer lines of code than usual, but they are a bit unclear, even for more experienced programmers. I am interested in your mathematical expression for the limits. I am just finishing some stuff here and I will expand it out. On a note, I think you can make it work with fewer variables. Why, for instance, having d and e? Couldn't it be just like: j = ((n-first*b[2])/b[1]) + 1? Try to use a better name for your variable than b, also helpful for someone eyeballing your code. P.S.: Try to post your code at codepad or pastebin. It's a lot easier to read there than reading here, and here it messes up with the indentation. :-)
The limits - yes thats the hart part and I'm not sure if I understand what is asked for - I think it is obvious that with larger amount of Nuggets in the 3 size packeges the larger will the largest number of McNuggets that cannot be bought allso be - My code only looks for the largest number within the set of integres wich is set in the function -there is no message if you exceed the number choosen
Maybe I fund out what you mean by limit - in the first problem the question is why it is true that if you can buy x, x+1,…, x+5 sets of McNuggets, for some x, then it is possible to buy any number of McNuggets >= x. So in my understanding it is the smalest Nug package size that is acountable for this limit - am I right? if so I got at bug in the code posted and I've fixed it. Also I deleted the variables you sugested.
Join our real-time social learning platform and learn together with your friends!