2015年2月8日星期日
Week 5
Tracing recursion can be very tricky. Last week in quiz, we were asked to trace a count_element function. I traced it like this:
Trace ce([15, [3, [5, 19], 32], 17])
--> sum([1, sum([ce([3, [5, 19], 32])]), 1])
--> sum([1, sum([1, sum([ce([5, 19]]), 1]), 1)])
--> sum([1, sum([1, sum([1, 1]), 1]), 1])
--> sum([1, 4, 1])
--> 6
It's really hard to imagine every step of recursion, I got a headache every time I finished a question.
This week Ta told me it's fine to just write the value, like this:
Trace ce([15, [3, [5, 19], 32], 17])
--> sum([1, 4 ,1])
--> 6
This is so much easier. But mistakes can be made easily if we don't trace it step by step and fully understand the recursion, just like my classmate @samnzhang did in the quiz, he wrote:
Trace ce([15, [3, [5, 19], 32], 17])
--> sum([1, 1, 1, 1, 1, 1])
--> 6
This is not how recursion works. The function didn't turn all elements into 1 at same time, it did so step by step.
We also had the term test 1 this week. For me the test is pretty doable. But I spent a lot of time on writing docstring. The reason I lost time on writing docstring is when I was doing assignment or labs, I tended to write code first, and then write the docstring after all needed code were written. If I don't need to hand in the code, or show it to anyone, I just didn't write docstring. From now on I need to write more docstring to be familiar with them.
This week in lecture, we covered a qucik sort algorithm. It was hard to trace at first, I spend a lot of time on it. Recursion can really make the algorithm very efficient.
订阅:
博文评论 (Atom)
没有评论:
发表评论