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.