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

Sveip for å vise menyen

book
Recursion

Recursion in Python is when a function calls itself. It's a useful technique for solving complex problems by breaking them down into smaller parts. The key elements of recursion are the base case (the termination condition) and the recursive case (where the function calls itself).

1234567
def print_message(message, times): if times > 0: print(message) print_message(message, times - 1) # 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.

Oppgave

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.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 5. Kapittel 1
Vi beklager at noe gikk galt. Hva skjedde?

Spør AI

expand
ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

book
Recursion

Recursion in Python is when a function calls itself. It's a useful technique for solving complex problems by breaking them down into smaller parts. The key elements of recursion are the base case (the termination condition) and the recursive case (where the function calls itself).

1234567
def print_message(message, times): if times > 0: print(message) print_message(message, times - 1) # 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.

Oppgave

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.

Løsning

Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 5. Kapittel 1
Switch to desktopBytt til skrivebordet for virkelighetspraksisFortsett der du er med et av alternativene nedenfor
Vi beklager at noe gikk galt. Hva skjedde?
some-alt