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

problem3 on problem set #2 ive written a code that I dont understand why it wont print the answer. Thanks for any help. http://pastebin.com/r8iTAsgd

OpenStudy (anonymous):

Your function starts with num_nuggets equal to 0 so in the next line: for a in range(0, num_nuggets+1): the range is (0, 1) which means 0 up to, but not including 1. So basically 0 to 0 which does nothing. Start with this. Hope it helps.

OpenStudy (anonymous):

ok i wrote this program: http://pastebin.com/NkJVSpM1 and i think it worked....(got answer of 15). thanks for the help

OpenStudy (anonymous):

actually this is totally wrong. It keeps adding 1 to the counter until it hits six, but it doesnt reset if it finds a wrong answer in between. however, I do not know where to put the else: x = 0 clause because it seems to reset the counter for just simply a wrong combination but not necessarily that there is no combination for a given number. any ideas ><

OpenStudy (anonymous):

can you post problem here, please?

OpenStudy (anonymous):

can you post problem3 on problem set #2 here or give link to it, please?

OpenStudy (anonymous):

ah i think i figured it out. adding the line elif a == num_nuggets: x = 0 solves this problem and i get 37

OpenStudy (anonymous):

What about 43?

OpenStudy (anonymous):

is 43 a real number? darn it...where did i go wrong

OpenStudy (anonymous):

My list of impossible to create numbers is: [1, 2, 3, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 22, 23, 25, 28, 31, 34, 37, 43] I checked 43 by hand to be sure.

OpenStudy (anonymous):

ahh interesting when i added :print num_nuggets below x = x+1 i got this at the end of the list 35 36 36 38 39 39 40 41 42 37 for some reasing i am getting x+2 for some variables...ill have to check why

OpenStudy (anonymous):

i solved it but i think it's weird method :/ http://pastebin.com/TuyPzPBb

OpenStudy (anonymous):

thanks tomas ill look at it now

OpenStudy (anonymous):

it's also correct for 4th part, you just need to change numbers :D

OpenStudy (anonymous):

tomas, in my program when i write num_nuggets += 1 my intention is to skip to the next number in the top range. does this command actually do this or does it simply change num_nuggets in a different way still in the same range? here is a paste of my most recent code: http://pastebin.com/H0VZsHcC

OpenStudy (anonymous):

i don't really get what you do but increasing num_nuggets by 1 doesn't affect loop where it used

OpenStudy (anonymous):

in other words...if i take that line out my coutner will increase 3 times if lets say there were 3 combinations for 30 peices of nuggets, however I only want the counter to go up 1 for every number of nuggets regardless of how many combos there are per number

OpenStudy (anonymous):

like if there was a goto command i would wrint x = x+1 goto top of range and next number

OpenStudy (anonymous):

you want to make that all of a and b and c would increase by 1?

OpenStudy (anonymous):

no, that the top line: for num_nuggets in range(1,60) will move to the next number and end the current loop its on

OpenStudy (anonymous):

once ive found that there is one possible combo i dont care if there are more...i just mark down possible and move on

OpenStudy (anonymous):

ah then you need to use break

OpenStudy (anonymous):

where do i put the break to have it break all the way up to the top range

OpenStudy (anonymous):

I am not sure how to break all loops

OpenStudy (anonymous):

maybe something like this I haven't tested it http://pastebin.com/231Mf2xY

OpenStudy (anonymous):

its something wrong with my code....ill look at it some more even though i cant stand the sight of it

OpenStudy (anonymous):

erthbound0, I'm not sure how far along you are now, but consider the following: combo = [] for c in range(0, 6): for b in range(0, 6): for a in range(0, 6): num = 6*a + 9*b + 20*c if num not in combo: combo.append(num) print sorted(combo) This should generate all the combinations you need. Now loop through the results keeping count of consecutive numbers.

OpenStudy (anonymous):

oh yeah that's better than mine didn't know you can do not in list

OpenStudy (anonymous):

Here is my solution: #Agdgdgdgwngo's MIT OCW 6.01 ps2 solution #I didn't bother commenting this script since it's so straightforward. nlower = input("This Python script prints solutions to the following Diopha\ ntine\nequation\n\n6a + 9b + 20c = n\n\nn lower bound = ") nupper = input("\nn upper bound = ") x = (nupper/6)+1 y = (nupper/9)+1 z = (nupper/20)+1 nlist = range(nupper+1) nelim = [] npossiblesol = nlist[nlower:nupper+1] nsol = [] testtype = raw_input("\nPrint every single possible value of n with a, b,\ and c, or simply n?\n[y for all solutions / n for just n]") for a in xrange(x): for b in xrange(y): for c in xrange(z): n = 6*a+9*b+20*c if n in npossiblesol: if testtype > "n": print (a,b,c,n) else: nsol.append(n) npossiblesol.remove(n) if nelim.count(n) is 0: nelim.append(n) if testtype <= "n": nsol.sort() print nsol else: for i in nelim: npossiblesol.remove(i) del i print"\nThe values of n with no solution in"\ ,[nlower,nupper] ,"are", npossiblesol print"\nLargest number of McNuggets that cannot be bought in exact quantity: "\ ,max(npossiblesol) del x, y, z,\ npossiblesol, nelim, nlist, nsol, nlower, nupper, testtype, a, b, c Here is it's output: This Python script prints solutions to the following Diophantine equation 6a + 9b + 20c = n n lower bound = 0 n upper bound = 100 Print every single possible value of n with a, b,and c, or simply n? [y for all solutions / n for just n]n [0, 6, 9, 12, 15, 18, 20, 21, 24, 26, 27, 29, 30, 32, 33, 35, 36, 38, 39, 40, 41, 42, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100] The values of n with no solution in [0, 100] are [1, 2, 3, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 22, 23, 25, 28, 31, 34, 37, 43] Largest number of McNuggets that cannot be bought in exact quantity: 43

OpenStudy (anonymous):

I wrote it a loong time ago, which is why it's quite ugly

OpenStudy (anonymous):

y'all should really use a code pasting site http://dpaste.com, http://pastebin.com, http://codepad.org, http://ideone.com .........................

OpenStudy (anonymous):

while im sure ya'lls programs are infinitely better than mine I frankly have no idea what they mean because I am still a noob supreme. I have spent 24 hours looking at this program trying to figure out what I wrote. Is there a l337 programmer out there who can help me with my program? I beleive the error lies in the code num_nuggets += 1 is not doing what i want it to do, namely ending the current number of mcnuggets and moving onto the next one: here is the program once again: http://pastebin.com/VrBmj4AA thanks to anyone who can help me enjoy my thanksgiving so i can get my mind off this program

OpenStudy (anonymous):

i took out some optimizations in case they were a problem - you can add them back when it is working,; commented out line 15 and changed line 16. i didn't test it http://pastebin.com/vQSqxKBB line 15 is unnecessary - the for statement at line 3 handles that

OpenStudy (anonymous):

@bwCA no this site should have [code]lalal[/code] implemented

OpenStudy (anonymous):

I would really recommend if you cant figure it out what you've done in 24hours for such a small program to just forget what you've written and try it again from scratch. It's quite a strange method you've chosen to do I must admit.

OpenStudy (anonymous):

I've rewritten your code using the method I think you were attempting: http://pastebin.com/RUFjmEW9 Let me know if that makes sense.

OpenStudy (anonymous):

strobe thanks...this did correct it and it makes sense. appreciate it

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!