Recursion saves a lot of work for computer scientists, it's a function calls itself. We did some exercises on input and output numbers using recursion. The very basic example nested list illustrated how recursion works in a simple and elegant way. I feel that recursion is a very interesting and convenient tool compare with iteration. However, tracing recursion was a bit confusing. We must trace the function calls in order, just like trying to understand how a computer thinks when calling recursion, step by step. For example:
-
Trace nd([5, [[1, [2, 3], [4]]]])
nd([5, [[1, [2, 3], [4]]]]) --> 1 + max([nd(x) for x in [5, [[1, [2, 3], [4]]], ’ox’]]) --> 1 + max([0, 3, 0]) --> 1 + 3 --> 4
Practice. Exercise.
No comments:
Post a Comment