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

Ok, ps2. 1 &2, were fairly easy, but I've got a block for #3. I've got tons of commented 'tried and failed' code Any feedback would be appreciated http://pastebin.com/6aNYUV0q

OpenStudy (anonymous):

I noticed that you're using lots of things like x, y, a, b, c... try replacing those with meaningful names, like sixpiece, ninepiece, numbnuggets, yadda yadda. (BTW yeslist and nolist are good names). That will make a mistake stand out more clearly. It would also make it easier for everyone else to read the code- they wouldn't have to guess at what you meant by "a". I see you were creating two lists. I approached it differently; I created one list of possibilities. Then at the end I printed what wasn't in my list, i.e. what isn't possible. So it's sorta redundant to create two lists. If I'm following it correctly, lines 12 through 16 will make the nolist consisting of all numbers 1 through 8 excluding 6, right? And lines 17 through 20 put "6" in the yeslist. Seems like a long way to go- can't you just hard code those in the list to begin with & avoid these loops?

OpenStudy (anonymous):

I ran your code. I tested for 30 nuggets. (or 30 boxes- are you counting nuggets or boxes?) The yes list was: yes [6, 24, 27, 44, 65, 68, 83, 86, 124, 127, 130, 132, 135, 138] The no list was: no [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 66, 67, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 84, 85, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 125, 126, 128, 129, 131, 133, 134, 136, 137, 139, 140, 141, 142, 143, 144, 145, 146, 147] I'm seeing lots of yeses that are in the nolist (if we're counting nuggets). And I believe Line 21 iterates from 1 through 100 regardless of what n is- seems like you want to iterate through n, whatever n is. I think you want to check your math between lines 24 through 27 (if we're counting nuggets). If you're counting boxes, then you still need to convert it to numbers of nuggets and print that.

OpenStudy (anonymous):

Oh, and BTW all these comments refer to the uncommented code you posted.

OpenStudy (anonymous):

cool thanks!

OpenStudy (anonymous):

http://codepad.org/Ygjg0YK9 This is my solution for problem 3. I feel it's a pretty good way to solve the problem. If anyone has any suggestions or improvements let me know! it's always helpful to get criticism. I also want to thank thearchie as I used his code for problem 1 as a base (though i changed some variable names)

OpenStudy (anonymous):

I noticed a problem with my script when I tried to use it in problem 4. The second for loop breaks, but the first one doesn't causing errors. I'm trying to think of a way to break them both if the if statement is true, but I'm not sure how to implement that.

OpenStudy (anonymous):

import android droid = android.Android() def mcdiophintine(number): var6 = 6 var9 = 9 var20 = 20 for num6 in range(0, number+1): numtot6 = var6*num6 if numtot6 == number: return True #print numtot6,' found in numtot6' for num9 in range(0, number+1): numtot9 = var9*num9 numtots9 = numtot6 + numtot9 if numtots9 == number: return True #print numtots9,' found in numtots9' for num20 in range(0, number+1): numtot20 = var20*num20 numtots20 = numtot20 + numtots9 if numtots20 == number: return True # print numtots20,' found in numtots20' return False number = input('enter a number ') if mcdiophintine(number)== True: print number, ' is a diophintine' else: print number, ' is not a diophintine'

OpenStudy (anonymous):

I analyzed the problem and optimized the solution a bit as a fun exercise: http://codepad.org/j00DeKlY

OpenStudy (anonymous):

Nice piece of code there walkerp1! How you derived the logic you used is beyond me lol but very nice optimization there.

OpenStudy (carlsmith):

import android??

OpenStudy (anonymous):

Nice Paul. Are/were you in the actual class? I noticed you didn't have any collaborators... I was trying to work some of that logic in, but I couldn't do it and have the result be a more concise script.

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!