A recursive method shuould have at least two properties:
1.the base cases
2.rules that reduce all other cases toward the base case
It is useful to deal with a complex problem with countless cases. We can just simplify the complex cases into the easiest one by repeating the same method, just like peeling onions.
Another example of recursion in reality is that we put two mirrors face to face, and put a candle in the middle of them, then we can see countless candles in mirrors. It can be regarded as a recursion.
As for the Python we are learning, the Tower of Hanoi is a good example to understand.
On one hand, recursion can help us solve many complex problem, on the other hand, it is some kind of inefficient, for each process, it calls itself many times, so that it have much data to deal with until get the final result.
Anyway, from what we are learning now, recursion can be said as one of the most practical method.
I think the base cases are so important. Once, I forgot to write a base case and it reads 'maximum recursion depth exceeded'
ReplyDeleteI think so, that is also a problem always occur to me.
DeleteThis comment has been removed by the author.
ReplyDeletevery useful!
ReplyDeletei think it's brilliant to imagine recursion as peeling onion and the face-to-face mirror kindle~
base cases are important and also the rules to reduce general cases to base cases!
Thanks for sharing!
Haha, thanks a lot ! :)
Deletenormally, we use base case and construction case to denote the recursion, which is the exact same word we use for induction.
ReplyDelete