Recursion is an unavoidable key concept in programming learning, and many beginners often feel confused when they first encounter it. So, what exactly does recursion mean?
Recursion is a method where a function calls itself. Typically, recursion breaks down a complex problem into smaller subproblems until a base case is reached (i.e., it no longer calls itself and directly returns a result).
A recursive function usually has two main parts:
-
Base condition: When a certain condition is met, recursion stops, and the function no longer calls itself.
-
Recursive call: The function calls itself while solving the problem, but the scale of the problem gradually decreases.
A classic example of recursion is calculating the factorial, defined as:
def factorial(n):
if n == 1: # Base condition: return 1 when n is 1
return 1
else:
return n * factorial(n - 1) # Recursive call
Next, let's use a real-life example to understand recursion:
The boss calls the secretary:
"Get ready for the weekend! We are going on a business trip for two days."
The secretary immediately calls her husband:
"Dear, I am going on a business trip with the boss, you have to take care of yourself for the next two days."
The husband smirks and calls his girlfriend:
"Baby, my wife is on a business trip, come over to my place, we can have some fun!"
The girlfriend's eyes light up and she quickly informs the boy she tutors:
"You are free this weekend, the tutoring is canceled!" The boy jumps up happily and immediately calls his dad:
"Dad, I can finally spend this weekend with you!" The boss hears his son's words and instantly softens, picking up the phone to call the secretary
and says:
"The business trip is canceled, I want to spend the weekend with my son."
The secretary sighs and calls her husband:
"We're not going, we'll stay home this weekend."
The husband is anxious and quickly messages his girlfriend:
"Sorry, my wife is not going on the trip, the plan is canceled!"
The girlfriend helplessly calls the boy:
"Sorry, the tutoring continues, hurry up and prepare the tuition."
The boy looks distressed and calls his dad:
"Sorry Dad, I still have to go to class..."
The boss sighs deeply and silently picks up his phone to call the secretary again:
"Well, get ready for the business trip..."
Implementing the above story in Python:
def phone_story(state):
characters = [
"The boss calls the secretary and says: 'Get ready for the weekend, we are going on a business trip.'",
"The secretary calls her husband and says: 'I am going on a business trip with the boss for two days, you need to take care of yourself.'",
"The husband calls his girlfriend and says: 'My wife is on a business trip, come over, we can have some fun.'",
"The girlfriend calls the boy she tutors and says: 'You are free this weekend, no tutoring.'",
"The boy calls his father and says: 'Dad, we can finally spend this weekend together.'",
"The father (boss) calls the secretary and says: 'The business trip is canceled. I want to spend the weekend with my son.'",
"The secretary calls her husband: 'I'm not going.'",
"The husband calls his girlfriend: 'Sorry, my wife is not going.'",
"The girlfriend calls the boy: 'You need to pay the tuition.'",
"The boy calls his father and says: 'Sorry Dad, I have to go to class.'"
]
print(len(characters))
print(state)
if state == len(characters):
print("The father calls his secretary, the story loops back to the beginning...\n")
return
# Progressing layer by layer
print(characters[state]) # Recursive call, simulating each person passing the phone step by step
phone_story(state + 1) # Backtracking, characters start changing decisions
print(characters[len(characters) - state - 1])
# Start from the first phone call in the story
phone_story(0)