These weeks, I have learned a lot about recursion function.A recursion function is a function inside calling itself repeatedly until a desired value is achieved.
I found recursion is so abstract that it is the most difficult concept for me to master.
The Turtle and the nested number list are two examples of recursive function. To understand how they work, one way is to assume the function works correctly when we call it for an order 0 level, then we just need to focus on order 1 level. If they work properly, we take it to the next level. It means we can trace the recursive function every recursion it runs.This method is really helpful for me to understand how recursive functions work. Moreover, the basic structure of recursive function is usually if statement ( if: ... else:), and the function is repeated in else code unless it reach a point or value which satisfy the requirement in if code.
In my Assignment 1, recursive function is the key step to reduce steps that moves cheeses from source to destination. In the lecture, I knew the code for tower of hanoi, and this code is the basic code for move cheese function. I choosed i according to the given function, and moved n-i, i and n-i cheeses using tower of hanoi separately. Finally, I calculate the total steps which is much less than n^2 -1.
This week, we were introduced a new data structure called Tree. A common kind of tree is binary tree, like a family tree, its top node is called parent, and the nodes it refers to are its children.Tree are defined recursively like other recursive functions. The process of assembling a tree is similar to the process of assembling a linked list. There are three traversal methods pre-order traversal, in-order traversal and post-order traversal.First print the root, then print the entire left subtree, and then print the entire right subtree. This way of traversing a tree is called a preorder, conversly,in-order is to print the left tree, root and then the right tree.Post-order is from left tree, right tree to the roots.
I acquired a lot of knowledge about the recursive function, and I am getting used to using it.
2014年2月22日星期六
2014年1月23日星期四
Slog Object-Oriented Programming
Although I took CSC 108 last term, CSC148 is still a big challenge for me. Especially, I had almost no confidencewith the Object oriented programming because Class is the weakest part I ever learned in CSC 108. In the first lecture, I had a short review on Class and a new things was introduced to me that was list comprehension. It can be used to created a new list by expressing every element in a list. This is method is so convenient that it really helps me simplify procedures and save time. At that time, I realize that CSC148 must be full of fun and I could do well on it as long as I work hard. However, the first week lab frustrated me a lot. During the lab, I was totally lost because I had no idea what to do. Thus, I asked my partner for help, and he taught me a lesson that read the handout and instructions carefully before you do anything. I suddenly understood that the reason why I was confused about what to do next was the lack of patience on reading. Following the instructions, everything became much easier, and with of assistant of TA, I also knew how to implement a class appropriately. However, I did not have enough time to accomplish all the work, but this lab help me review lots of material from CSC108 such as files, writing function, string manipulations. One thing really built up my confidence was that I accomplished exercise 1 without any help. At first, I ran my class program and I always got the error. In order to figure out the problem, I read my program step by step and tried examples to test each functions. Then, I find I did not use string method to represent the word in class, it was the key error caused the whole program failed. Finally, my program ran successfully and passed the tests, to be honest, it was really a achievement for me. In conclusion, I find that it is so meaningful to do python work, and I believe that I will do better in the future.
订阅:
博文 (Atom)