2014年3月30日星期日

Sorting and Efficiency


I learned a little bit about sorting in csc108. I just learned three easy ways of sorting, bubble sort, insert sort and select sort. They are very easy to understand, and all of them do not use recursion. For example,

def select(L):
    """Produce copy of L in sorted order by selection sort
    >>> select([3, 1, 2])
    [1, 2, 3]
    """
    L1 = L[:]
    for i in range(len(L1)):
        m = min(L1[i:])
        j = L1.index(m,i)
        L1[i], L1[j] = L1[j], L1[i]
    return L1

this is the code for select sort. Choose the minimum number in the list from index i to the end, swap that minimum number with the first number at index i. by using for loop, we can get a sorted list.

In csc148, I learned some new ways of sorting and knew how to determine the time complexity. the time complexity is commonly expressed using big O notation. It is related to the length of the list. The average time complexity for select, insert and bubble sort is O(n^2). Quick sort and merge sort are two new ways of sorting. For example,

def quick(L):
    if len(L) > 1:
        pivot = L[0] # there are much better ways of choosing the pivot!
        smaller_than_pivot = [x for x in L[1:] if x < pivot]
        larger_than_pivot = [x for x in L[1:] if x >= pivot]
        return quick(smaller_than_pivot) + [pivot] + quick(larger_than_pivot)
    else:
        return L

we divide a list in three parts (smaller than pivot, pivot and larger than pivot). Then use recursion for the parts smaller than pivot and larger than pivot. The time complexity can be O(n^2). The possible case can be that every number in the list is same.  Merge sort is a little bit different from quick sort. Firstly, we need to implement a function to put two lists together. And we also need a function to divide the list into 2 same length lists. The complexity for merge sort is same with quick sort. Merge sort and quick sort both use recursion. They both divide the list into parts than put them together. Every way of sorting has its own features. When we solve some computer problem, everyone will write different code.

2014年3月24日星期一

week10


The topic of this week’s lecture was about sorting. In csc108, I learned about selection sort, insert sort, bubble sort. Every ways of sorting has its own features, some of them are efficient. This week I learned quick sort and merge sort, and both of them use recursion. The complexity of each method is different. it's my first time that knowing big oh. it tells us the growth rate.

The lab of this week was about inorder traversal and the longest path. When I did these two functions, I used helper function to make implement easier. After drawing binary trees and discussing, we finished this lab quickly. Recursion is our friend. It makes our functions easy to understand.

2014年3月16日星期日

week9


This week was a little busy. I needed to finish my assignment 2 and exercise 3. Especially for assignment 2, it is difficult. When I got the handout, I even did not know where I should start. After asking question on piazza and discussing with my partner, I was on the right track. Nearly every function needs the recursion. For example, in is_regex, I need to write helper functions to distinguish different types of regex. In the regex that has the form ‘(‘ + ‘r1’ + ‘.’ + ‘r2’ + ‘)’, I need to know whether r1 and r2 are also regexes, so this part needs using recursion.

The lab of this week let me know the importance of using recursion. When we wrote the function count_less, it was easy to implement by using recursion. However, if we are not allowed to use recursion, we need to think other ways. It took me much time to get the result. I used the class Stack to save the numbers. Comparing with using loop, recursion is easier.

2014年3月8日星期六

week8


The most important thing I learned this week is about linked list. The linked list class has some features of list, and it is like the nested list. However, the linked list only has two elements, one is the node, and the second is the linked list. When I added some methods of the linked list, using recursion is the most efficient and the easiest way. During the lab, my partner and I solved some problems by drawing pictures which is very helpful for us to understand and implement the code. This week’s lab was much easier because it was just the extending of the last week. The lab of this week also helps me understand how __getitem__, __setitem__ and __delitem__ work. When we added some methods such as insert or reverse in linked list, it is also a chance for us to practice using recursion. The lecture of this week was a little bit difficult, and I need time to understand all of them. I got a pretty good mark of midterm, and I will continue to work hard and experience the programming.    

2014年3月2日星期日

week 7 recursion


The topic of this week is about recursion. In my opinion, recursion is one of the most difficult parts in csc148, but it is also the most useful part. It can help us make the code simpler and convenient. At the beginning of this semester, I started to get some knowledge of recursive function. When I did the assignment 1, using recursion is the most important part. I needed to decide when I should use the recursion and understood how recursion works. For example, moving cheeses with 4 stools needs us to think about how to move cheeses with 3 stools, how to get the result with the smallest steps, and how to use the destination stool and intermediate stool correctly. In addition to the moving cheeses, getting the greatest common denominator and binary representation both are about recursive function. Recursion helps us solving problem more efficiently because we don’t need to write too many codes.

The lab of this week is about linked list. My partner and I didn’t finish. We spent too much time writing long and difficult codes and when we ran it, always got error. Using recursion to get the length of a linked list is much easier.

This week I also learned something about Fibonacci sequence in math lecture. Recursion helps me understand the sequence well. Recursion is not only used in computer science, but also used in math. I know recursion is very helpful, but does the recursion have some diadvantages?

2014年2月16日星期日

week6


This week was pretty good. I finished my assignment 1 with my friend and got the expected results. The lab of this week was pretty easy just like I did in csc108. It was about comprehension, loop and unit test. The lecture of this week was about tree lists. It was a little bit difficult that need me using recursive function and imaging what the tree looks like.

The assignment 2 was post when I just submitted my first assignment. This assignment is more abstract and I even do not know where I should start. During the reading week, I will make an effort to understand this assignment.

2014年2月8日星期六

week5


This week was busy. I had to take other course’s test. In addition, the assignment1 will be due on next week. The lab of this week about recursive functions was not difficult but needs we think deeply.  It was useful for doing assignment. Binary representation and getting greatest common denominator are two interesting functions. They are not only helpful for computer science but also mathematics.

Yesterday, I got stuck in the step5 of the assignment. After I read the lecture slide of this week and searched something about Hanoi, I worked out and finished Tour class. This step was the most difficult one. Lots of recursive functions needed to be written.

The lecture of this week was not difficult, but I still felt confused about scopes and namespaces.