Thursday, 27 February 2014

Week 7 - Recursion

Recursion is a confusing little thing but once you know how to use it its kind of beautiful. Here I will highlight a few of my favourite examples.

The first is simple. A function called 'sum_list' might say this:

return sum([sum_list(x) if isinstance(x, list) else x for x in L]) where L is the list or object.

When I first looked at this, I was perpetually confused and thought it was the stupidest thing on earth. The way I read it in my mind, was "if x is a list then return the sum of the sum_list" which made absolutely no sense at first because wasn't sum_list what we were trying to write in the first place? How was this function going to take the sum_list of something if sum_list was the function being created?
After some time I came to realize that the essential part of this function lies in the else statement. If x is a list, then sum_list will repeat itself, but now the items in the list are being looked at individually. So if the items inside the list isn't a list, sum_list will only look at the item and will add it to the sum. It's wonderful.

Another example of recursion is as follows (excuse the poor stylistic elements):
def remove3s(L: list):
I = 0
while I < len(L):
   if isinstance(L[I], int):
      if L[I] == 3:
         del L[I]
         continue
elif isinstance(L[I], list):
         remove3s(L[I])
I = I + 1

This function strives to remove all 3s from a list L. It takes into consideration the possibility that L[I] might be another nested list, so it calls itself if that's the case. When it calls itself, it takes the nested list as the new L and goes through each element in the nested list.

Recursion is also useful in the class Tree:
def leaf_count:
if len(t.children) == 0:
   return 1
else:
   return sum(leaf.count(c) for c in t.children)

Something is a leaf of a Tree if it has no children. In Trees, though, it's not just as simple as there being parents and children, because children can have children who can have more children. In this case, because Trees multiply in such a repetitive way, and each Tree contains more Trees, recursion is necessary. This method says that if a tree has no children, return 1 because this means there is one leaf. However, if a tree does have children, these children might be leaves themselves so the function has to go back and also see if the children have any children.

And that pretty much sums up my little blurb on recursion!

Tuesday, 18 February 2014

Reading Week

It's Reading Week (finally) so I have begun to study for the upcoming term test.

So far I have gone over the first three weeks of lectures, including articles and python examples. Mostly, my reaction has been, "Wow, why didn't I go over this sooner? Maybe then that exercise or that lab would not have seemed so perplexing". Basically, I did not practice enough when we were actually learning this stuff for the first time. Oops.

Some interesting things I have learned that probably everyone else already realized (don't laugh at me):
- creating a __str__ method in a class means you can call str(instance) and print(instance) and they will actually return what you want them to
- Zip creates tuples of pairs that are at the same index in their respective groups (I feel like this method would have been useful in 108 instead of all those 'if s[i] == t[i]...')
- When creating init methods for subclasses, you actually have to say 'superclass.__init__(self) to get it to refer to the superclass. It won't automatically do this for you.

Things I am still confused about:
- unittest. I looked at the Stack Tester example and noticed it's a little different from the unittest file my partner and I created in a lab a couple weeks ago.
- What it is that makes attributes of a class changeable by users and what makes them immutable
- How __repr__ method calls itself recursively in a lot of the classes we created in class

I'm having one of those "the more I study the less I feel like I know" kind of days. Nevertheless, I plan on solving these issues by exploring the info on the python website and trying things out in Wing. Also, I'll probably head over to office hours when I'm back on campus.

Saturday, 8 February 2014

Week 5 Post

This week we got some hints and explanations with regards to Assignment 1. We saw how the Tour of Hanoi game actually works when you execute it and how many steps you really need to get the task done. It was a big help because I would probably have been lost without that visual aid.
The lab was all about recursion. It was good in the sense that I was able to learn how to actually call functions within themselves. We were pretty much able to complete the lab on time too, which was a bonus.

Week 4 post

This week's lab was alright. Besides the really tedious math involved, it required use of subclasses and exceptions. I was happy about this because I remembered subclasses from 108, so there wasn't anything too new for me there. 
In class, we learned about recursion. At first I thought I got the list-comprehension part. The example given, which was about finding the sum total of nested lists, seemed easy enough to understand. The only part that really gave me a hard time was the idea that the nested lists all evaluate in one step. 
Later, though, I realized that recursion also involves the function calling itself, and that really confuses me still. I'm going to go over the examples from class again to understand it.