Friday, February 6, 2015

Entry 3: Week 4 - Recursion & Term-Test

     The main topic of this week is called Recursion. It is essentially breaking up a problem into smaller sub-problems until the problem can be solved immediately with no required steps. At first, it seemed fairly straight-forward, thanks to Professor Heap's example of the function "sum_list". This function takes in a single parameter and if it is a number, the function will simply return that number. However, if it is a list of numbers, it will return the sum of the numbers in that list. Recursion is designed so that if this list of numbers contains a nested list of numbers, the function will break up the list into smaller calculations.
This week's lab was based on tracing the Recursion process of a particular function. I received a mark of 1/2 because I missed the main focus of Recursion which is to break down the function as much as possible. The function was the following:

def ce(L):
     """(list of int) -> int
     """

   
     if isinstance(L, list):

        return sum([ce(x) for x in L])

     else:

        return 1

One of the tracing problems was the following:

Trace ce([15, 5, 3, 32, 19])

My first reaction was to take the sum of all the numbers in the list because according to the function definition, if the argument is a list of integers, it should return the sum of that list. However, I realized upon inspection, I misinterpreted the function definition. If each element in the list is not a list, it will return 1. Therefore, the solution to this problem would be the following:

ce([15, 5, 3, 32, 19]) -> sum([ce(x) for x in [15, 5, 3, 32, 19]])
                       -> sum([1, 1, 1, 1, 1])
                       -> 5

I believe tracing Recursion requires a little more practice and hopefully, I can master this practice in the coming days.

     Another important highlight to note is the term test. It consisted of three questions on Recursion and the construction of classes. It was a fair test and thanks to the extra practice I had with tracing Recursion, I was completely confident on what I wrote in each question. One of my friends (http://jinsooparkcsc148.blogspot.ca/) had problems with tracing Recursion, but I am sure he understands the main idea of it, which is to break down a function into smaller processes. Overall, this week has been the busiest so far this term and according to Professor Heap, he will teach how to code Recursion in the coming weeks.

No comments:

Post a Comment