This week we discussed more about recursion in class. A
recursive function is a function that can call itself during its execution when
certain condition is met. The numbers of the Fibonacci sequence can be thought
as a sequence of numbers that can be calculated recursively. Each Fibonacci
number is the sum of the previous two numbers, except the first two numbers which
equals to 0 and 1. With the help of programming, we too can easily find the
Fibonacci numbers using recursion.
def fibonacci(n):
if n == 0 or n == 1:
return n
elif n > 1:
return fibonacci(n - 1) + fibonacci(n - 2)
This function above
is a simple example of recursion function that can calculate for the first few
hundreds of Fibonacci numbers. Once the recursion reaches a stage of
calculating two Fibonacci number when the maximum recursion depth is reached,
python would automatically return a RuntimeError. Recursive function can raises
errors unexpectedly if we don’t pay attention to what test cases our code can
and cannot handle.
During the lab this week, we worked on inheritance,
exception, and writing test cases using Unittest. On the day I had my lab, I
took most of the time to evaluate the correctness of my code and writing test
cases. Although I had a better understanding of how I can use unittest after
the lab, I figured I have to start review and practice programming more often
to catch up with the course.
No comments:
Post a Comment