Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Recursion | Recursion and Lambda Functions
Python Functions Tutorial

Scorri per mostrare il menu

book
Recursion

Note
Definition

Recursion is a programming technique where a function calls itself to solve a problem in smaller parts. It's especially useful for problems with repetitive structure or natural sub-problems.

The key elements of recursion are:

  • Base case: the condition that stops the recursion;

  • Recursive case: the part where the function calls itself with a simpler input.

Why use recursion?

Some problems can be naturally expressed in terms of smaller subproblems. Recursion provides a clean and elegant way to solve these by having a function call itself to handle the simpler cases. It is commonly used in tasks like processing trees, exploring paths, or breaking down structures (e.g., lists, strings).

1234567
def print_message(message, times): if times > 0: # Base case: stop when times is 0 print(message) print_message(message, times - 1) # Recursive case # Function call print_message("Hello, Recursion!", 3)
copy

Let's go step by step through how this recursive program works:

  1. Condition Check: if times > 0, the function proceeds. In this case, times = 3, so the condition is true;

  2. Print Message: the function prints "Hello, Recursion!";

  3. Recursive Call: the function calls itself with times - 1;

  4. Repetition: this process continues until times equals 0;

  5. Termination: when the condition times > 0 is no longer true, the recursion stops, and the program completes.

Result: The message "Hello, Recursion!" is printed three times.

Understanding the flow:

Each time the function calls itself, it adds a new frame to the call stack (a memory structure that keeps track of active function calls). The function keeps calling itself with a smaller times value. Once it reaches the base case (times == 0), it stops. Then, each previous call completes one by one in reverse order. This backtracking behavior is essential to how recursion works.

Compito

Swipe to start coding

Given a string representing a phone number, which may contain spaces, dashes, parentheses, or other non-numeric characters. The goal is to extract only the digits using recursion.

  1. If the input string number is empty, return an empty string.
  2. Check if the first character of the string number is a digit using the isdigit() method in an if condition.
  3. If it is a digit, concatenate it with the result of a recursive call to format_phone_number, passing the substring starting from the second character.
  4. If it is not a digit, make a recursive call to format_phone_number, skipping the first character.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 5. Capitolo 1
single

single

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

close

Awesome!

Completion rate improved to 4.35

book
Recursion

Note
Definition

Recursion is a programming technique where a function calls itself to solve a problem in smaller parts. It's especially useful for problems with repetitive structure or natural sub-problems.

The key elements of recursion are:

  • Base case: the condition that stops the recursion;

  • Recursive case: the part where the function calls itself with a simpler input.

Why use recursion?

Some problems can be naturally expressed in terms of smaller subproblems. Recursion provides a clean and elegant way to solve these by having a function call itself to handle the simpler cases. It is commonly used in tasks like processing trees, exploring paths, or breaking down structures (e.g., lists, strings).

1234567
def print_message(message, times): if times > 0: # Base case: stop when times is 0 print(message) print_message(message, times - 1) # Recursive case # Function call print_message("Hello, Recursion!", 3)
copy

Let's go step by step through how this recursive program works:

  1. Condition Check: if times > 0, the function proceeds. In this case, times = 3, so the condition is true;

  2. Print Message: the function prints "Hello, Recursion!";

  3. Recursive Call: the function calls itself with times - 1;

  4. Repetition: this process continues until times equals 0;

  5. Termination: when the condition times > 0 is no longer true, the recursion stops, and the program completes.

Result: The message "Hello, Recursion!" is printed three times.

Understanding the flow:

Each time the function calls itself, it adds a new frame to the call stack (a memory structure that keeps track of active function calls). The function keeps calling itself with a smaller times value. Once it reaches the base case (times == 0), it stops. Then, each previous call completes one by one in reverse order. This backtracking behavior is essential to how recursion works.

Compito

Swipe to start coding

Given a string representing a phone number, which may contain spaces, dashes, parentheses, or other non-numeric characters. The goal is to extract only the digits using recursion.

  1. If the input string number is empty, return an empty string.
  2. Check if the first character of the string number is a digit using the isdigit() method in an if condition.
  3. If it is a digit, concatenate it with the result of a recursive call to format_phone_number, passing the substring starting from the second character.
  4. If it is not a digit, make a recursive call to format_phone_number, skipping the first character.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

close

Awesome!

Completion rate improved to 4.35

Scorri per mostrare il menu

some-alt