This
week, we almost finished recursion. I really enjoy thinking about questions
during the lecture. It let me feel that I am following with professor. I did learn
a lot of things about how recursion works. This is a new topic for me and it is
quite challenged. For example, the tower of Hanoi taught me a lot about
recursion. Function can run by calling itself. Before 148, I never meet this
kind of things. At the start, it confused me a lot because I thought the
function will run over and over. It never stopped. However, I am wrong. It would
never stop if there was no conditions or limits for the function. After I study
more about the question of tower of Hanoi, I realized that there is always some
situations that make the function stopped. Another thing that makes me confused
is the return thing. Specifically, if I do ‘return min([nested_list[x]]) if isinstance(x,
list) else x for x in L, L = [1,2,[3,[4,5]]]’, I thought it will just return ‘1’
because the first one is ‘1’ and then it will end this function. However, after
I trace the function it shows me this. The first one is ‘1’, it is not a list
so it will return ‘1’. Then return means to remember this ‘1’ will be store in
a list. This is an easy example. After I did the example of winter 2013 term
test Q4. I understand more about it. This question is like this:
def
rec_len(L):
"""(nested list)
-> int
Return the total number of
non-list elements within nested list L.
For example, rec_len([1, 'two',
[[], [[3]]]]) == 3.
"""
I
did this:
result = 0
for x in L:
if isinstance(x, list):
result += rec_len(x)
else:
result += 1
return result
Every
time the ‘return’ remembers the number of the non-list elements. If it is a
list, the number of non-list elements will be stored in the result and then
count the next one and so on. This makes clear for me that how recursion works.
I mean the assignment 1 and tower of Hanoi for me is like a practice of imitating
the same pattern. I did understand those examples in lecture and try to do the
same way but I did not understand it in my own way. After those examples, I learnt
more things which belong to me. However, more things emerged: how could I know
if a function is recursive. I can do it by feeling. In another words, Guess!
Yes, assume it is recursive and try to do it see if I could do it in recursion
or not. Some question is obviously recursive such as nested lists. But maybe some
are not; hard to say. I felt ok with recursion. I mean I can face to the
problem and solve it by myself. I just need time to think about it.
Four
stools tower of Hanoi is a really good assignment that related to some internet
source. I searched some websites that solve the problem of four stools. Some
people gives the mathematical function of four stools, some people gives the
data of i that in minimum moves, number of cheeses, and minimum number of
moves and some people do it in java or other programs. I cannot understand that mathematical function completely nor the java codes but I understand
some of the explanation. But I did not use it, it is just some hints that help
me to consider my recursion function.
The
biggest achievement that I had is that I can do some questions by myself in a
few changes. The python shell is a good way to test myself. I wrote the code
then I test it by setting me some testcases. Some edge cases were also
included.
This
week’s tutorial was going good. Learning new things and sharing ideas with partners.
I mean at the start I was talking with the tutorial group mates about the test.
I did not finish all the questions in the handout. And I also realized that
this Linklist is very important so I try to finish this at home. Good news is I
finish it. Bad news: I get some questions. I will ask those questions in office
hour or to my friend.
Q:
how can we stop a recursion function?
A:
setting a condition, when it satisfies this condition, it will stop.
No comments:
Post a Comment