banner
andrewji8

Being towards death

Heed not to the tree-rustling and leaf-lashing rain, Why not stroll along, whistle and sing under its rein. Lighter and better suited than horses are straw sandals and a bamboo staff, Who's afraid? A palm-leaf plaited cape provides enough to misty weather in life sustain. A thorny spring breeze sobers up the spirit, I feel a slight chill, The setting sun over the mountain offers greetings still. Looking back over the bleak passage survived, The return in time Shall not be affected by windswept rain or shine.
telegram
twitter
github

Don't understand recursion? No worries, this conversation between the boss and the secretary will make you understand it in no time!!!

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:

  1. Base condition: When a certain condition is met, recursion stops, and the function no longer calls itself.

  2. 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:

image

def factorial(n):    
    if n == 1:  # Base condition: return 1 when n is 1 
        return 1   
    else:       
        return n * factorial(- 1)  # Recursive call

Next, let's use a 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'm going on a business trip with the boss, you'll 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 in joy 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.

He says:
"The business trip is canceled, I want to spend the weekend with my son."

The secretary sighs and calls her husband:
"Not going anymore, I'll be home this weekend."

The husband is anxious and quickly texts his girlfriend:
"Sorry, my wife is not going on the trip anymore, plans are canceled!"

The girlfriend helplessly calls the boy:
"Sorry, tutoring continues, get ready for the tuition fee."

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:
"Ah, 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'm 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 to my place, we can have some fun.'",
        "The girlfriend calls the boy she tutors: 'You are free this weekend, no tutoring.'",
        "The boy calls his father and says: 'Dad, we can finally spend this weekend together.'",
        "The father (the 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 anymore.'",
        "The husband calls his girlfriend: 'Sorry, my wife is not going anymore.'",
        "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)  # During backtracking, characters start changing decisions
    print(characters[len(characters) - state - 1])

# Start from the first phone call of the story
phone_story(0)

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.