Ask your own question, for FREE!
Computer Science 13 Online
OpenStudy (anonymous):

Struggling with a written task in 6.189 A Gentle Introduction to Programming, namely 1.15 in here http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-189-a-gentle-introduction-to-programming-using-python-january-iap-2011/assignments/MIT6_189IAP11_hw1_written.pdf. I can see many inconsistencies alright, but I don't understand what could be a valid guess of the original purpose of this code. Or do I have to simply rewrite it in a way that avoids infinite loop? Any ideas/experiences?

OpenStudy (e.mccormick):

Well, as for what the code does, lets walk through it. We set two variables, n and i. Generally speaking, n is some number and i is some iterator. You would work with or on the number, and how many times you work would be determined by i. Though, those generalities do not hold in every case. while i > 0: Ah ha! It looks like i is the iterator. So n would, presumably, be something we would work on. However, n never comes back again... so I would say the lack of using n is one of the bugs. Next we have i printed, OK, then do the real work of the loop with a conditional statement. If i can be divided in half without a remainder, do so. Otherwise, increase i. So I will be cut in half, fail and be added to, cut in half, fail and be added to, repeat. Eventually it hits 1 and does a 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, infinite loop. The goal, if there was one beyond making a bug, probably had something to do with evaluating some range that was half of n. Each time, it could chop it in half. But the i was used, rather than the n, causing issues. Also, in that presumed idea, the i would need to be a decrement counter. Once it zeroed out, the look would stop.

OpenStudy (anonymous):

Thanks for such a helpful and detailed reply @e.mccormick. I like the way you described not using n as a bug by itself. Because I found it weird and inconsistent, but I didn't think of it as a bug, while it might be a good habit to call such things their proper names. It was also very kind of you to try and find some sense in this code, because I was tempted to think that this guess-and-fix part of the task was kind of a mock task to make students realise that they should provide some comments. But actually the very attempt to find some sense behind nonsense makes a good challenge to write a code which does exactly what you said. And I'm going to try it.

OpenStudy (e.mccormick):

Here are a few possibilities of things based on the buggy code: http://dpaste.com/hold/1276626/ A couple of them make a little more sense, but it is still basically just junk code. It seems to server no mathematical application or other practical use.

OpenStudy (preetha):

@anaskoy Be sure to post your question in the 6.189 subgroup of Computer Science. It gets tagged and other 6.189ers will help you.

OpenStudy (anonymous):

@Preetha, good point, thanks!

OpenStudy (anonymous):

Thanks @e.mccormick. It's only now that I could afford to get back to this task and your solution versions and I found them extremely helpful. Namely, I realised in what way for loop and while loop can be interchangable in terms of setting the number of iterations. Really thank you, I've just rewritten my for-loop task in this way https://gist.github.com/ansakoy/5895141

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!