Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Recursive functions
#1
Is it really important to fully know Recursive functions to be able to be a good coder? This thing is eating up my head to be honest.
Reply
#2
I think it's a technique you should know, yes. Certain problems are expressed more naturally in terms of recursion (e.g. searching trees depth-first).
Reply
#3
Most programmers use recursion so often that it becomes second nature. To program recursive algorithms, simply program as if you already knew how to do something. For example
1
2
3
4
5
How to find all of a person's living ancestors?
    * find all that person's mother living ancestors
    * add all that person's father living ancestors
    * if the mother or the father is alive, add them
    * return all these ancestors
This is a typical recursive algorithm: I suppose that I know how to find the mother's ancestors or the father's ancestors, which I don't actually know how to do, but I'm simply going to call the algorithm recursively.

The problem is that the above algorithm never stops, we need to add a condition to avoid going into an infinite loop. For example we can add at the beginning:
1
* If the person was born more than 150 years ago, they have no living ancestors, return the empty set
So the program is written without effort!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020