Tuesday, 24 March 2015

Week 11, 0316-0322

Looks like my count of week number differs from the course website's as I counted reading week in. Shouldn't be too much trouble to figure out though.
This week we looked at assertion and testing a bit. Nothing seems particularly counterintuitive, the materials left to be covered are more of small bits here and there after all.
Assignment 3 is posted. I think I prefer option B, likely because ideas explored in option B seems to form the basis of those in option A. As of now it's going alright, I'll try not to land myself into an "find a problem near the deadline, panic and submit 5 minutes before the deadline" scenario again...

Tuesday, 17 March 2015

Week 10, 0309-0315

Strike is still going on, labs are still cancelled, not much to write about.
We had a midterm this week, so there's only one lecture further into binary trees. The lecture was mostly about implementation of insert and delete on binary trees, which are slightly messier than the methods we've seen before, especially the delete one which has a whole lot of cases.
The midterm went fine, although I may or may not forgot to write one of the cases for the last problem. My memory is faded.

Monday, 9 March 2015

Week 9, 0302-0308

This week was uneventful other than that labs were cancelled because of the strike. We finished up on assignment 2 and talked a bit about iteration, but didn't cover much.
I thought I finished assignment 2 early, but realized around an hour before it's due that the minimax strategy doesn't work properly. Again cue frantic revising. Beating the deadline by five minutes was a bit too thrilling for my likings, but I suppose it worked out fine this time.
Iteration is somewhat similar to recursion in that it also repeats a part of a task until the task itself is done, although iteration is more of repeating a smaller (parallel) portion of a task until the loop condition fails, while recursion reduces the task into simpler and simpler cases until the base case is reached. We used linked lists as an example for iteration, where objects (nodes) in the list are linked to one another, so iteration works well here. We define a "front" node and move along the list, changing our current node, to achieve whatever we want to do, say check if an object is contained in the list.

I've been reading some of the other slogs, but don't really have much to say other than occasionally giving solutions to problems. Not sure how to work on that part.

Monday, 2 March 2015

Week 8, 0223-0301

Reading week is over and we are back to class.

We talked about binary trees this week, which is basically a special type of tree which has at most two children. As such it'd be inefficient to organize the children of binary tree nodes into lists, and using two attributes for the "left child" and the "right child" for each node seems like a good idea.
Binary trees, with their special property, seem useful for representing and evaluating binary expressions, as we can describe them using such trees with each leaf as a value to be operated upon, and two leaves' common parent as the operator to perform on the left and right leaves. This way, by recursively going down the tree, joining the operators and values, we would be able to easily obtain the multilayer binary expression represented by the tree and evaluate it.
This concept is also helpful for searching for values, as an ordered binary tree (where the left child of a node is always less than the value of the node itself which is in turns less than the right child of the node) can direct the search to one path, so that we don't have to examine the nodes that are, by the orderedness of the tree, clearly not the value we are looking for. This means we can halve the search scope every step we go down the tree, so it takes at most lg n steps to search a tree with n nodes (meaning the worst case performance is O(lg n)), which seems much more efficient than a linear search where the worst case performance is O(n) steps.

Assignment 2 is going okay. I finished the tippy game and am working on minimax. I've found myself scrapping all my work and restarting more than once during this assignment though. I'm not sure if that's a good sign or not.

We are supposed to give a summary for recursion, object-oriented programming, or abstract data types for this week. I've already done the first two, but I think I'll add a few more words to the recursion one, as it seems that the TA didn't like it--I'm honestly not sure what the TA is looking for though. I'll just give a general summary of how it works.
What we've been working on for recursion is basically defining the function's behavior on a base case where everything else can be reduced to such a case, and by calling the function itself within the code for the function, reduce other more complicated cases to the base case. I once heard someone saying recursion can be explained as "an onion is an onion skin with an onion inside". We don't particularly care about how many layers of skin the onion has (well, as long as it's finite), as recursion does the work for us, we only need to define exactly how the function peels the onion skin, how the function operates on the core of the onion, and how the function reconstruct the layers of onion skin back into an onion, or into whatever we want it to be.
For example, a function f that operates on objects in lists can be applied to objects in nested lists using something as shown below:
def g(l):
    if isinstance(l, list): # when the object operated upon is a list
        return [g(i) for i in l] # reduce to a list of one less layer
    else: # base case
        return f(i) # f is the function directly applied onto the objects in the list
Here, the function g is an application of recursion, which reduces nested lists to nested lists with less layers and eventually to non-list objects which it passes to f. The reconstruction of the onion here is just putting the onion skins (list brackets) back up. So the outcome is the original list with each non-list object in it being operated upon by f.
We can do something else with the reduced case though, say if we change g into:
def g(l):
    if isinstance(l, list):
        return max([g(i) for i in l])
    else:
        return f(i)
the outcome would be the maximum value of the function f applied to each non-list objects in the list. Of course, here f has to be some function that outputs a string or a numerical value.

I've talked about applications of recursion, and as the binary tree example discussed above is also an case of the applications of recursion, I suppose there's no need to restate how useful it is.