Everything works fine so far. The recursion is definitely a hard concept to understand which requires some imagination while writing it, but meanwhile the required imagination is what makes recursion interesting.
TA and Professor are so helpful. While explain the recursion, the Professor trace the function to let us understand how recursion works and I found out that using tracing to explain recursion is really helpful since it turns abstract concepts into concrete imagines. So whenever I encounter some recursion codes that I cannot really understand, I usually trace it.
While working on recursion by myself, I found out that except using imagination and tracing, understanding the base case, which is when the recursion is going to stop and return something, is fairly important. So when I am writing the recursion code, what I usually do is to spend some time to analyze the function and the design the base cases.
Below is a example of recursion code that I am working on to practice.
def count(obj):
‘’’
Count how many items are in the list
‘’’
#Base Case 1
if not obj:
return 0
#Base Case 2
If not isinstance (obj, list):
return 1
return sum(count(obj[0]), count(obj[1:]))
没有评论:
发表评论