Wednesday, April 6, 2016

Week 12

This is the last week in CSC148. During the last two weeks we mainly discussed the efficiency of the code (i.e. steps take to finish running the code), and hashing. It is interesting to know that we can calculate the height of the tree by lg2(number of nodes )+1. However, the most intriguing part of the last lecture is that we are taught about some python function outcomes we previously were unaware. For example, 

L=[[]]*3

L#->[[],[],[]]  different objects create L 3 copies

L[0].append(1)

L#->[[1],[1],[1]]


Here the L =[ [ ] ]*3 is just creating 3 copies of L and prints out [ [ ] , [ ] , [ ] ]. But when we 
append something to it, all the lists in the list will be added with the new something. 

In addition, we learned how to count the size of the function represented by O(n). For 
example, a n-times loop that contains a n-times loop will have size of O(n^2)

The hard thing I found about O(n) is when the loop runs differently. I have to get more practice
 so that I will get a better sense of it.

The final exam is close, I think it is important not to leave any unanswered questions and 
attend help center.

It have been a nice semester and I am looking forward to next year.

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!

Monday, February 29, 2016

Week 7

This week I am going to discuss the Abstract Data Type.

Abstract Data Type, or ADT, refers to the data type defined only by its behavior and class of objects whose logical behavior is defined by a set of values and a set of operations. The STRUCTURE is usually meant to be inherited and is composed of no instances. The difference between abstract class and abstract data type is that the class does not provide definition for all of its methods and methods should be implemented in subclasses while the data type is merely a model of a certain kind of data structure.

Two popular examples of abstract data type are Stack and Queue, which respectively implies the rule of Last in First out (LIFO) and First in First out (FIFO). To clarify the ambiguity (which I initially had as I am an English learner), last in first out is like storing food in a draw; the ones you put in the last are the ones you eat first. On the other hand, First in first out is as trays in KFC restaurant because consumers take out the top ones, which are also the first ones that the employees inserted. Queue and Stack are two useful features that many class inherit. Each of them has a class list instantiation.

One good example of class that inherits Queue would be the dispatcher class in our first assignment. In the dispatcher class, the dispatcher needs to assign customs and drivers in order and the first come first serve rule is always obeyed. Without the Queue data structure, it would be hard to achieve such order. 

In short, Abstract Data Type is useful data structure that provides bases for many other classes and thus facilitates coding.   


Friday, February 26, 2016

Week 6

Back from reading week and ready to restart.

This week we discussed recursion, which is a fascinating topic as it enables solving complex questions. Although some function can be expressed without using recursion, the form of the recursion facilitates the speed of the function. The following example can be written in both recursive and non recursive 


def factorial(n):
    """    
    Recursive function computing the factorial of n 
    """
    return t
    if n == 0:  ###recursive 
        return 1    else:
        return n * factorial(n - 1)


During the week's tutorial, we practiced converting filtering and list comprehension to loops. I personally find that introducing filter, zip, reduce and map will be a good topic for a lab because they highly reduce the time students take to write codes.