Tuesday, March 29, 2016

Week 11

On week 3, I talked about writing code in economic ways. But after I programmed for weeks, I found that making the code readable is also very important as our code will be used by clients in the future. Apparently we can't put a bunch of single letter variable in hope that they would understand. In addition, list comprehension is convenient but such feature only exists in Python and we need to get used to regular expression (I have to admit that my sense on regular expression has weakened since I left Java); but it's painful to see list comprehension when it's excessively long. Here, after doing research online, I made rules for myself regarding python coding:

1. Write docstring carefully and think of all the possible outcomes and explain the hard-to-understand code. In addition, the naming of the function should be clear (i.e. count_people).
Also the type of the input and output should all be defined. 

2. Try regular expression first and account for all possible inputs and outputs. Then, if I am confident about the code, I convert them to list comprehension (if it's in python) if it's not too long.

3. Learn all kinds of functions that perform the same task, so I get to know which ones are more handy and have shorter running time.

4. It doesn't hurt to write side comments!

5. Be careful to use recursion as it takes up lots of running time. 

6. Check pep8.

Moreover, It is important to ask opinions from others about the code we create.

Wednesday, March 23, 2016

Week 10

In this week's lecture we learned how to implement the delete node method for binary. In delete node we need to check the number of children the node has in order to proceed. If it has no children, we can simply make it None. If it have one child, we can connect its child to its parent. If it has two children, then we need to construct the connection such that left lower than middle lower than right is always achieved. In addition, we have acquired the idea of running time. For example x to the power of 5 is much faster than x to the power of 3. More importantly, we learned various sorting methods (though the default python one is the best).  For example, in quicksort ascending order, we choose the pivot and put the greater values to its right and lesser values to its left and in each left and right part, we do the same thing again until the last single value is reached. The most inefficient method is likely bubblesort because it checks the value with the next value and swaps them if the current value is smaller; it's a disaster if the list is in descending order. I found learning different methods of sorting very fascinating.

Wednesday, March 16, 2016

Week 9

This week we get a little deeper into binary tree and I feel much more confident when writing recursive codes, especially the ones for tree classes. I think the most important part is collecting experience. After I practice implementing various tree methods and see different ways of writing, I got to understand which parts I need to care about. For example when we start implementing the recursive codes, we need to first think of based cases, i.e.  thinking of what will happen if root is none and what will happen if the root has no children.

Here is an example:

def parenthesize(b):
    """    
    Return a parenthesized expression equivalent to the arithmetic    
    expression tree rooted at b.
    """
    if b is None:
        return ''    elif b.left is None and b.right is None:
        return str(b.data)
    else:
        return something complicated 

It is like that we need to give every condition an environment to live. True the function 
automatically returns None when it meets a None, but it is still clear to provide the 'what if '
code. 

Wednesday, March 9, 2016

Week 8

This week we have finished general Tree class and started binary tree, which is a very efficient search method. For the Tree function, there were many terms that I initially got confused. i.e. Leaves, height (max number of edges), arity, depth, internal nodes. But as I worked more often on Tree functions, I found I got used to the terms and can now implement basic recursive Tree functions. The uniqueness for binary tree is that the maximum number of children for each node is 2. In addition there are four visiting order for binary tree: preorder, inorder, postorder and levelorder. Let us denote parent, left, right as p, l and r. Then the sequence for each order is respectively plr, lpr, lrp and l->r from up to bottom. After I look at the binary tree implementations, I found it very difficult comparing to the recursive functions we have seen; it has various branches which made recursion harder. But I do like the idea of recursion and feel passionate about solving all of them. Also, I am also looking forward to learn more searching methods. I hope to improve my thinking speed by accumulating experience!