2014年3月27日星期四

Sort and efficiency

The process of this lesson is going to the final part. This semester, I learned further and deeper knowledge concerning Python programming. In the previous two weeks, we studied the serious kinds of sort problems, which is quick sort, merge sort, count sort, insertion sort and selection sort respectively. After this week's final lab, I got some useful conclusions involving the efficiency of each sort algorithm. Insertion, selection and bubble sort are all based on O(n^2). Moreover, bubble sort is the simplest method to sort series,but it is with the worst algorithm in the fundamental sort cases because this algorithm need to check every two-continuous groups in the series. As the results, those three sort methods ,insertion, selection and bubble, are very original way. Another three sort methods, except count sort, are based on O(lg(n)) and similar to use binary method. Quick sort is a unstable sort when I do some research on the Internet. Actually, it's average worse performance is exactly O(lg(n)). However, when encountering the worst case where is useless to use binary method, it returns to  O(n^2). On the contrary, merge sort is a stable method and always under O(lg(n)). In my opinion, this is the most efficiency case of those fundamental sort methods. The last sort method called count sort is like a linear method according to O(n+k). This sort algorithm only uses restricted series that must obey to continuous numbers. After calculating sketchily, series with less than  approximately 100 elements has more quick speed than the algorithm that based on O(lg(n)).

2014年2月10日星期一

For A1 feeling

For whole week, I was struggling for my Assignment 1. Now I have almost done this relatively huge assignment. As viewing whole process of implementing online schedule, it is the practice for using class from step 1 to step 4 and the recursion, the extreme key part of whole program, is in step 5 and step 6. At that time for fixing the algorithm of Hanoi Tower, I did the research for resolving this historical problem. Although the method for moving is quite easy, it is unknown question whether it is the most efficient way to solve problem if changing with different numbers of stools. As you known, n-1 cheeses should be moved to the spare stool if it is the fewest steps of three stools. Whereas, the fewest steps for four stools is not made n-1 cheeses to spare stools. After my testing continuously, k, n-k cheeses, should be 3 and I found the shortest steps for 8 cheeses in four stools is 33. However,I have a very weird problem here. It would give me an error on 12 cheeses, in total, doing automatically. The most strange thing is that everything is fine except this number. The algorithm that I have written is on the following part.
def move_cheeses(self, n: int, source: int, intermediate1: int,
                     intermediate2: int, destination: int) -> None:
        if n > 2:
            self.move_cheeses(n - 3, source, destination, intermediate2,
                              intermediate1)
            self.move_cheeses(2, source, intermediate1, destination,
                              intermediate2)
            self.move_cheeses(1, source, intermediate1, intermediate2,
                              destination)
            self.move_cheeses(2, intermediate2, intermediate1, source,
                              destination)
            self.move_cheeses(n - 3, intermediate1, source, intermediate2,
                              destination)
        elif n > 1:
            self.move_cheeses(1, source, intermediate1, destination,
                              intermediate2)
            self.move_cheeses(1, source, intermediate1, intermediate2,
                              destination)
            self.move_cheeses(1, intermediate2, intermediate1, source,
                              destination)
        else:  # just one cheese --- no recursion required!
            print ('Move disk from', source, 'to', destination)

2014年2月2日星期日

Recursion

As the day gone, the course goes further and deeper. We studied how to use unittest on lab,by the way, it was extremely useful for me to solve the CSC108 unittest formation problem, and the recursion problems with turtle model last week. From the study and the exercise, I comments some reasons of using recursion to handle problem. Firstly, the length of programming code with recursion would be less and shorter than with for or while statements,the old methods. Secondly, it is more flexible than those two statements, because those two statements have the specific formation, whereas, recursion calling itself again and again to hit the target is easy to modify and transform. The third one is to change the difficult problems to the simple-solving problem. Taking our A1, the tower of hanoi, for example. The algorithm of the game seems to be difficult to tackle if using for or while statements. However, after several times of tracing the steps, it is just unimaginable easy method when selecting recursion. However, it is said that the sword has the two sides. if want to use recursion, the method or the algorithm should be very clean. Sometimes, one easy recursion would be spent much more time on finding what it is and how to write the code by tracing. I am still struggling for the A1. Most parts (approximately 70%) have been done.

2014年1月22日星期三

Slog: : Objected-Oriented programming

Beginning of CSC148 course, we started to the class unit that continues from the end of CSC108 course. Firstly, I thought that class in Python is just another way to write functions in Python. Whereas, I made a fundamental mistake, which is that class is more efficient than function, after following three weeks. Class also has more official name, called Object-oriented programming (OOP). There are mainly existing objects, attributes and methods. According to my comprehension, objects are the instances of classes by using methods to associate and the attributes describe what the objects are. Compared to the function in Python, OOP with simpler code and space completes the work quickly and uses relatively less time to run the code. As three weeks gone, some problems concerning OOP appeared during the process of doing assignments, labs and reviewing the notes. One of the greatest problems is that I cannot determine when the attributes of instances or of classes and what is the difference between those. Now maybe I can explain and fix this problem by self-studying. The attributes of classes are t only for storing and saving the data that associates to the classes. The attributes of instances are the individual part. However, there are some links between those two parts. The attributes of classes can be called by using the instances if the instances don’t have the same variables’ names as the classes had. If the instances do have the same attributes’ names of classes, it should be the attributes of the instances when using the corresponding instances called the attributes. This is my explanation of this problem, maybe too complex to describe, just saying as clear as I can. Keep going on for myself!