Friday, January 31, 2014

Week 4- More Recursion

Tracing

In class, we used a nested example for recursion. We were expected to define nesting-depth of L as 1 plus the maximum nesting depth of L's elements if L is a list, otherwise 0.

We started off with writing the definition and return statement:
We did tracing in order to understanding how recursion works. In the lecture, we were given a few recursive calls to trace with reference to  nested_depth.

Trace   nested_depth([])
1 + max([] +[0]) -> 1
Trace   nested_depth(17)
Not a list -> 0
Trace   nested_depth([3, 17, 1])
1 + max([0, 0, 0] + [0]) -> 1
Trace   nested_depth([5, [3, 17, 1], [2, 4], 6])
1 + max([0, 1, 1, 0] + [0]) -> 1 + 1 -> 2

Trace   nested_depth([14, 7, [5, [3, 17, 1], [2, 4], 6], 9])
1 + max([0, 0, 1, 2, 2, 2, 2, 2, 1, 0] + [0]) -> 3

For a more complex one, we tried finding the maximum number of a nested list using:
This can further show how recursion works by tracing.
Trace   rec_max([3, 5, 1, 3, 4, 7])
max([3, 5, 1, 3, 4, 7]) -> 7
Trace   rec_max([4, 2, [3, 5, 1, 3, 4, 7], 8])
max([4, 2, 7, 8]) -> 8
Trace   rec_max([6, [4, 2, [3, 5, 1, 3, 4, 7], 8], 5])
max([6, 8, 5]) -> 8

Drawing Turtles

This week, we also looked at the tree_burst function and by using the Python Launcher

If we change our docstring example at the bottom into the following, what will happen?

The Python Launcher has helped me in graphing the first example and I tried by implementing the codes for second example listed above into the graph (in similar colours):




No comments:

Post a Comment