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.