Saturday, 7 February 2015

Week 5: Tracing Recursions





















Recursions is one of the most interesting topics I've learned in computer science so far. The topic is so fascinating and somewhat confusing to follow. The concept of a function calling itself is really amazing.Recursions depth can go infinitely deep, yet the code is a finite statement.
What exactly is recursion?
Well this is an example of recursion function.
def sum_list(L):

    ’’’ (list or int) -> int
    Return L if it’s an int, or sum of the numbers in possibly nested list L
    >>> sum_list(17)
    17
    >>> sum_list([1, 2, 3])
    6
    >>> sum_list([1, [2, 3, [4]], 5])
    15
    ’’’
    # reuse: isinstance, sum, sum_list !
    if isinstance(L, list):
       return sum([sum_list(x) for x in L])
    else: # L is an int
       return L
Basically if the item is an int, it will return the int number. If the item is a list, the function calls itself to check if there is nested lists or if there are more int within the list. Recursion helps the problem of determining how many nested lists are within the list. Recursion keeps calling the function as many times as the depth of the list. This can go on infinitely deep.
How would you trace the lists or int?
Take the example sum_list([1, [2, 3, [4]], 5]). Since L is a list, it will call the function sum_list again, and it will take the sum for every int in that list. Then, it will further check for nested lists within the list until it finds the last nested list in the list. Then, the all the int will be added up to the sum of 15. Keeping track and tracing recursions can be a little tricky and confusing at first. However, with more practice, you will be able to get the hang of it and trace recursive functions of higher complexity.

Week 4: First Impressions

The way CSC148 course runs is very different from CSC108. CSC108 was an inverted classroom teaching style, and it was very convenient for the lecture videos to be online. However, I like that we learn the material in class for CSC148 because I learn the best in classroom where I can listen to the professor directly. Especially since I did not have any previous programming experience except for CSC148, I think it's a lot helpful to learn the material in class and doing lab assignments to help me understand the content better. So far, we have been doing review on classes, but I still got to learn new material such as property and raising exceptions. I was fascinated with the class Turtle because I never knew you could draw in python. It's amazing because I love to draw and I can now incorporate drawing into python. I realized how flexible programming is because programming can be applied to almost everything.
This course is relatively enjoyable so far. I learned a lot about teamwork working on assignments with my group partner. Although working on assignments and keeping up with work is tiring, it was a lot of fun. The assignment was a lot more fun than the one in CSC108 because we actually got to create a simple game. I actually went to office hours a lot this semester compared to last semester, since I never went last semester. Now, I get to learn a lot more about programming from the professor and from my peers. Danny Heap is a great professor with a wealth of knowledge to share, and I also like how he tries to get to know all his students despite the huge classrooms he teaches. Although computer science was not the subject I planned on studying initially, I am really glad I chose this program.