Sunday, March 22, 2015

Impression on Week 9

    Well, for this week's class, we learned a little bit of assertion, talked a little bit of A3, and function tracing(exciting isn't it)!

    Assertion is just checking whether the user has inputed a result which may cause error in the function or not.  As helpful as it may sounds, it can cause a little problem on function run times.  Because it is checking every input on whether it is a assertion error or not, it has increased the run time of the functions significantly, which may not be a problem for some cases, but a disaster when it really eats away your time.

    Another thing we covered this week in class is tracing functions, like the one below.  Now, without copy and pasting it to see what the output is, take some time to really think about how it works.  What would print(h(1)) output in the python shell(answer at the end of the blog)?  It is really interesting right?

N = 5
M = 2

def f(N):
    return M * g(N + 1)

def g(N):
    return M * N

def h(N):
    return f(g(N))


if __name__ == '__main__':
    print(h(1))

    It is really shocking me how much time to have to spend on your own just to play around in the python shell, and how much stuff you can actually learn from it.  Learning by yourself is a big part of this course and hopefully I will see you next week on how the class is going.


Oh and one more thing, the answer is 12, it would carryout the g(1) in the f(g(1)) first which returns 2, and then f(2) which is M * g(3), g(3) would return 6, and M(which is 2) times 6 is 12.  Do you get it now? 

Impression on Week 8

    This week we learned more stuff on Binary Search Trees(AKA BST).  It is a tree which can only have up to 2 children.  The one on the left is less than the data in that node, and the one on the right is more than the data in that node, and we are going to learn how to carry out delete and insert method on it, including on how to implement the method ourselves.

    This is way more challenging than I thought it would be.  At first I thought it would just be like any other lab we did before, but without the TA's help, I was a little lost at first, but what is important is I found my way through.  

class BTNode:
    """Binary Tree node."""

    def __init__(self: 'BTNode', data: object,
                 left: 'BTNode'=None, right: 'BTNode'=None) -> None:
        """Create BT node with data and children left and right."""
        self.data, self.left, self.right = data, left, right


class BST:
    """Binary search tree."""

    def __init__(self: 'BST', root: BTNode=None) -> None:
        """Create BST with BTNode root."""
        self._root = root


     The code above is just a to give you a vague idea of what the init method of the code looks like.  As you might have noticed, it has two class, we are using the BTNode class to create BST.  It is kind of similar to what you might be familiar of, a helper function, but in this case, it is not a function, it is a class(so maybe you can call it helper class??).  We are using BTNode to implement BST which is easier to do in a sense of coding.  You will know what I mean once you get your hands on it.

    Well, so much for this week.  Let's behold what challenge lies head next week waiting for us to solve.
    

Sunday, March 8, 2015

Impression of week 7

    This week, we learned some linked node that we use in linked list, which is still a type of tree.  The following picture is a example of a linked list node.  Each of the objects has a value and a reference to other similar objects.  It is just like what you can see in the example, at the end of list it simply just refers to none.
    The linked list node is not all we learned.  We also learned how to use iterations on them, which is just looping, and there are also some mutations, which are updating objects.

    I guess it is harder to search for this week's tree, but doable.  We did not really use recursion this week, but some looping, old school stuff we learned how to do in 108.  I think it is a lot easier than recursion.  

    I do not know what next week is going to be, maybe some more challenging stuff??  This week's lab is somewhat harder because we do not get to ask the TAs for help, who are strike.  Hopefully we will get back to normal tutorials soon.

Monday, March 2, 2015

Recursion

    This week on recursion, we went into something called BTNode, a binary tree.  Each node can have up to two children and you can actually apply binary method to the data that is contained inside.  If the children is bigger than the node, it is going to be at the right of the node, and if not, it is going to the left.  

    Our job was to create method that evaluate the BTNode, in other words put all the data into application and add, multiply... every data. And also there is a method for representing what is in the tree...etc.
    
    Although there are a lot of similarities in the recursive code we wrote in the previous week, however, this week's code is much more complex.  Instead of getting the job done in four or five lines of code, we need to consider more cases this time, therefore, more lines of code.

    This week's lab took me a lot more time than the last week, mainly because I thought we only need to write the function in a few lines, but after I realized that we need a lot more lines to finish each function, everything becomes clear again.  

    I cannot say that this week's recursion is confusing because it is not.  Recursion code is made a lot clearer to me than when I first got my hand on it.  It is really interesting and I am really looking forward to put it to use in the newest assignment!