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

Hi everone! This is my first question here. I'm very glad to have found this website! I'm trying to solve Prob3 of pset2. I've looked for other people's solutions in the questions list (btw, is there any way to search a topic more efficiently than just scrolling down and reading?) The solutions I've seen uses def(). According to the course calendar http://bit.ly/ryklob def() is not needed to solve this, as long as is not teached yet in Lec3. So how could the problem be solved this way? This is my attempt but it's now working: http://codepad.org/IrPTcvOx Thank you very much!

OpenStudy (anonymous):

1 - As of now, you have to scroll down to search for questions. Alternatively, you can search for "problem set <number> mit openstudy" on google, something like that. 2 - Consider creating a fixed tuple for the package sizes (6,9,20). One way to solve it is to create nested loops to populate a list of possible values that can be bought, and then just looping over the list, something like "for num in xrange(60): if num not in list: tempMax = num" then at the end of the loop, print the tempMax. Might not be the more efficient way to solve it, but it works and it is an easy way to code it, at least for me. General tips: Never create such a long statement like that if statement. It's hard to read, and it may not be doing what it should do. Is it necessary? 9 out of 10 times you can code it more cleanly, or just not coding that part at all. If it's extremely necessary to write long statements (declaring a lot of arguments, for instance), write part of the statement in another line - Python doesn't care for blank spaces.

OpenStudy (anonymous):

Thank you very much, @bmp! I agree that, as long as openstudy does not have a specific search tool, the best option is to use google with "site:openstudy.com". I think that creating a list is, indeed, the best way to solve it. At least the best one I can figure out for the moment. But I was trying to do it only with tuples, because the concept of list is not introduced yet in the 3rd lecture (the same as with def() ), so I guessed they might want the students to do it that way. Moreover, in the pset2 pdf, they suggest to store "the last 6 consecutive values that in fact pass the test of having an exact solution". That is a more general way to solve the problem (althought I didn't do it correctly in my first attempt) since it is not dependent on any superior limit you have to give, but it establishes a general rule. Anyway, I am not able to find the way to solve it without a list, so this is my solution: http://codepad.org/nqHsPVQh Thanks a lot for the advice about long statements. I will follow it :)

OpenStudy (anonymous):

I think you misread their suggestion in the pdf. It doesn't say to store the last 6 buyable numbers. It says that you need to detect when you've found 6 consecutive buyable numbers. You should save the last unbuyable number you found, and when you've found a buyable number that's 6 larger than it, then you're done. Also, you should really try to do the problem first, before searching for other peoples' solutions. It's easy to read some code and understand why it works; it's a different skill entirely to write code to do a specific task. When you get really stuck then ask for help. I know it's tempting to just look online when you can't figure something out, but you really need to go through the exercise of fumbling through it to experience what works and what doesn't. The other issue, as you mentioned, is that your code will only work if you know the answer is less than 55 (less than 49, actually). You should be using a while loop and incrementing n indefinitely.

OpenStudy (anonymous):

dmancine... you are right! :P I did misunderstood the pdf. Your way to solve the "6 consecutives test" is much more elegant. Thanks a lot! I agree with you about trying to solve the problems before looking for other people solutions. And... that's what I did! In my first question, I copy my code. After realizing that it did not work, and that I was not able to find a solution, I came here to look for other solutions and finally ask. Thanks for the while loop tip. In working on it right now ;)

OpenStudy (anonymous):

*I'm working on 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!