2015年2月21日星期六

WEEK 5 JOURNAL

    In week 5, we talked about the stacks and sorts. We can use the method that we learned in week 5 to orient a list of numbers. This method also needs to use the recursion concept. First, we need to figure out where the first number goes by partitioning other numbers to its left or right. In this step, we let the numbers smaller than the first number go to the left side and the numbers bigger than the first number go to the right side. Then we take the same approach with the left and right partitions. Once the length of both sides are smaller than two(means there's no elements or only one element on both sides), we can return the list and the list is sorted.

    The body of this method is like this:

def quick_sort(L):
    if len(L) >= 2:
        return (quick_sort([x for x in L if x < L[0]]) + 
                [L[0]] + 
                quick_sort([x for x in L[1:] if x >= L[0]]))
  else:
    return L

  The reason why we can use recursion to sort a list is because once we have oriented a number, the elements on the left side are always smaller than the oriented number so that we can regard the left side part as a new list and do the same thing again ( because all the numbers in the new list are smaller than the oriented number, when we organize the new list, it won't mess the original list). Similarly, we can use the method to organize the numbers on the right side. Once we finished organizing left side part and right side part, we can get a sorted list. 

2015年2月15日星期日

WEEK 4 JOURNAL

    In week 4, we talked about how to use recursion to solve problems. The core of recursion is to re-use one 'def' for several times until the computer get the correct answer. For example, sometimes we want to calculate the sum of all elements in a list, but some elements are numbers and some elements are a list of numbers. In this case, we need to use recursion to write the 'def'. The def looks like this:

def sum_list(L):
      if isinstance(L, list): 
           return sum([sum_list(x) for x in L]) 
      else:  # L is an int 
             return L
    
    Consider this method, we find that this method re-can use itself (see 'return sum([sum_list(x) for x in L]) ', here it re-uses the sum_list method), and because of this, when some of the elements are a list of numbers, the method can split this list into numbers and then calculate the sum of the list. Therefore, we can get the sum of the entire list.

    Recursion is special because it can change different type of things into the same type of things by re-using the method. When we look at the body of the def, we can find that the use of 'else' and  'sum_list(x) for x in L' is to change different type of elements into the same type of elements, and the 'return sum' part is actually the part that can help us get the final answer. So when we want to use recursion to solve problems, we first need to think about how to transform those elements into one same type, and then once we finish the first step, we can think about how to get the final answer. 

    In conclusion, recursion is ingenious but complex, we still need to have a lot of practice on recursion so that we can be skilled in using recursion.

2015年1月25日星期日

WHY GEEKS SHOULD WRITE?

    The new semester begins, and we are asked to write slog for CSC148. I wrote several slogs last semester when I was taking CSC165 and I found that there are some advantages for students to write slogs. Here are some of my thoughts about why geeks should write.
 
    The most obvious thing is that write down something can help geeks organize their thinking. Since different people have different thinking, there are several ways in programming a code. In this situation, write down something can help us organize our thinking and analysis using which method is the best. Thus, write down something can help us find the easier method in programming and can help us make fewer mistakes than those who don't write at all.

    Moreover, write down something is another way communicating with other programmers. For example, once you write down something(like the slog), other programmers can read and others may get some inspirations from reading your writing. Similarly, you can also get some ideas after reading other people's writing. In this aspect, write something is a good way to communicate with others and get help from others.

    Further, for our programmers, sometimes we need to learn lots of new things, and write down something is a good way to help us understand and memorize new knowledge. Since you have record the new knowledge, it is also very convenient for you to review.

    Based on those advantages, I believe that it is a good habit for geeks to write down something.