Friday, February 14, 2014

Week 6

We finished Assignment 1 this week.  Finally!!!  It was a great practice on recursion and it was actually a lot of fun working on this assignment.
So this week we started a new class of object that seems to deal with a great amount of recursion --- the binary tree.  We first look at 3 different traversals --- the pre-order, in-order and post-order.  In all these three ways of traversals, we need to use recursion to complete the code.  It is actually quite straightforward to write these traversal codes.  The other functions are a little bit more complicated, including 'height', 'count', 'leaf_count', 'arity'.  List comprehension was so useful in writing these functions, along with the use of built-in functions like 'sum' and 'max'.  I found that it is very important to consider the base case in a recursion code.  For example, in 'count' function, we need to have a '+1' at the beginning so that a node without children would count. 


The lab this week is on converting 'tenary if' into regular if statements.  In general, the tasks are easier than any other previous labs.  We finished quite early.  Moreover, we got to practice on 'any' and 'all'.  These two are very efficient ways along with the use of list comprehensions.  They check each condition in the list comprehension -- 'any' returns True if any one is true; 'all' returns True if all are true.  It would've taken a lot more work to check the conditions one by one.  


No comments:

Post a Comment