Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Recursive functions
#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
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:
    * 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


Messages In This Thread
Recursive functions - by Ayman_2001 - Jun-24-2020, 01:46 PM
RE: Recursive functions - by ndc85430 - Jun-24-2020, 03:15 PM
RE: Recursive functions - by Gribouillis - Jun-24-2020, 03:21 PM

Forum Jump:

User Panel Messages

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