Since last week we had hand in our first assignment of this semester, we had a nice weekend that was not very busy, but we are quickly aware of a new task: the first midterm test is coming. We spent almost the whole Week doing reviews for the term test. We discussed together and figured out much knowledge that we didn't notice, or we can say, ignore the class. For example, we found that the concept of string and representation are almost the same, so we searched many websites and finally got the answer. Simply, string is written for a computer to read, and representation is used for a reader to comprehend. It was very important for us to totally understand them before the test since that can help us not mix their concept and write wrong codes.
Another thing we met this Week is recursion. Recursion is a way of programming or coding a problem, in which a function calls itself one or more times in its body. So we can use it to express some method that may have to run infinite times, such as calculating the amount of the branches of a tree.
We learnt how to trace recursion. Suppose we have a function:
def nested_list(L):
if isinstance(L,int)
return L
else:
return max([nested_list(x) for x in L])
and we have a list: lst = [3,[2,1]], we can trace it:
Firstly, we observe the function and find out what it means. Here it means judge if L is an int,and return it if true. So call the function [nested_list(x) for x in L] once, and we get 3 is an int and [2,1] not. The result is [3]. Call it twice, we have the result[3,2,1]. As the code says, we finally sum it, and the result 3+2+1=6.
Until now we can only read and find out the result of a recursion, but I believe that soon we will learn how to write a correct recursion on our own.