Friday, February 27, 2015

Week 7: Summary of Recursion

Before going to what I did this week, I want to go over the details on the recursion which I will be talking about. In my opinion, recursion is a brilliant way for one to be able to call the function inside the function to perform and apply the function to nested lists and its elements. Judging from what I had learnt so far, there are two types of recursion. One is a recursion involving  list or non-list. I will use the example from my week 5 SLOG entry to talk about this.

def length(L):
    """ (list or non-list) -> int

    Return the max length of the list (possibly nested list) in L.

    Pre-condition: L should be Python object either list or non-list.

    >>> length([1,2,3])
    3
    >>> length([1,[2,3,4]])
    3
    >>> length(12)
    0
    """
    if isinstance(L, list):
             return max([len(L)] + [length(x) for x in L])
    else:   # if L is not a list.
             return 0

Trace length([1,2,3,4])
--> max([len([1,2,3,4])] + [length(x) for x in [1,2,3,4]])  # since L is a list.
--> max([4] + [0, 0, 0, 0])
--> 4

Trace length([1,[2,3,4]])  # nested list recursion tracing example
--> max([len([1,[2,3,4]])] + [length(x) for x in [1,[2,3,4]]])  # since L is a list.
--> max([2] + [3])
--> 3

For this type of recursion, programmer has to consider the situation when L is non-list, thus how it will answer the function. The above, example, function length returns the max length of the list, usually, I have used both max, min and sum methods to create a recursive code. Of course, I also should not forget the case when L ,which is our parameter for function length, is a list. By implementing isinstance(L, list), computer immediately check if L is list type Python object. This method "isinstance( )" was one of methods which I have never encountered before, so it was really interesting for me to see how easily I can check whether something is list, int, float, or str.

When one talk about a recursion, you should not forget about tracing a recursion. As one of the examples provided, tracing a recursion shows a clear representation of how the recursive code will perform in various situations. Also when one is required to trace a recursion, a programmer needs to think about all the possibilities that might need to be consider. For function length example provided above, I need to consider the following cases:

- When L is non-list such as string, or integer.
- When L is a list but with no nested list
- When L is a list with nested list
- When L is an empty list
- When L is a list with nested list and nested nested list.

Usually, giving multiple tracing examples which have a different conditions enable for people to recreate a recursive code even without a docstring of the function.

Unlike a recursion involving a list or non-list, the second recursion involves the use of tree. For a tree, you can have a several leaves and internal nodes. This is actually what I have learnt during this weekend. I will use one of examples from this week's lab to explain the difference with the first recursion that I talked about. Please revisit lab 06 (Copyright goes to Danny Heap and Diane Horton) just in case how t which is a part of BTNode to understand the definition of t.left (node or leaf located on the left side of the root t) and t.right (node or leaf located on the right side of root t).

def height(t):
    ''' (BTNode) -> int

    Return the number of nodes in longest path in BTNode t.

    >>> tree = BTNode(23) #please refer to lab06 from CSC148 for any information. 
    >>> height(BTNode)
    1
    >>> b = BTNode(8)
    >>> b = insert(b, 4)
    >>> b = insert(b, 2)
    >>> b = insert(b, 6)
    >>> b = insert(b, 12)
    >>> b = insert(b, 14)
    >>> b = insert(b, 10)
    >>> height(b)
    3
    '''
    if t is None: # this means if there is no root, internal node and leaves. 
         return 0
    else:
         return 1 + max(height(t.left), height(t.right))

(Source: http://www.cdf.toronto.edu/~csc148h/winter/Labs/lab06/additional.py)


Unlike a recursion involving a list, when involving tree, you have just have to call the function without having to use "function(x) for x in L". At first, since I was only familiar with a recursion involving list or non-list, it was a little bit difficult for me to understand. However, with enough practices and help of T.A., I was able to get my code to work properly.

After reflecting on what I know and learn about a recursion, I begin to wonder if there is another type of recursion which is totally different from two recursions I talked about above. However, there is one certain thing that a recursion is also one of valuable basic programming lessons one have to take not just because it uses a lot in programming, but it also gives students a new insight on what computers are really capable of.

As for the summary of Object Oriented Programming, there is nothing new I learnt so far, so please refer to week 6 entry to find out what it is about.

I guess I already talked about most of things I have learnt this week. One thing I found easy about this week's lecture is the definition of leaves and internal nodes. Since I already memorized what these terms mean and how they work, it was really easy for me to follow a course materials. Now I will continue on my CSC148 assignment 2 which also involves a recursion.

I also commented on some of SLOGs that I felt it is good. Bottom links are some of SLOGs I had commented:

-http://chrisc148slog.blogspot.ca/2015/02/entry-3-recursion-recursion-recursion.html?showComment=1425095301984#c6525962159396058506
-http://sarcasticgradientdescent.blogspot.ca/

Just as a side note,  today I learnt about there is going to be a strike. I hope both parties, UofT and CUPE3902 unions can find a reasonable terms to avoid a long-term strike.

1 comment:

  1. I highly agree with your post. Honestly, you explained as well as Danny himself! This was very descriptive.

    ReplyDelete