Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to make the recursive function call itself.
DSA Python
def countdown(n): if n <= 0: print("Blast off!") else: print(n) countdown([1])
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Using n leaves the function stuck on the same number.
✗ Incorrect
The function should call itself with n - 1 to count down correctly.
2fill in blank
mediumComplete the code to calculate factorial using recursion.
DSA Python
def factorial(n): if n == 0: return 1 else: return n * factorial([1])
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Using n does not reduce the problem size.
✗ Incorrect
Factorial multiplies n by factorial of n - 1 until reaching 0.
3fill in blank
hardFix the error in the recursive Fibonacci function call.
DSA Python
def fibonacci(n): if n <= 1: return n else: return fibonacci([1]) + fibonacci(n - 2)
Attempts:
3 left
💡 Hint
Common Mistakes
Using n + 1 causes infinite recursion.
Using n does not reduce the problem size.
✗ Incorrect
The function should call fibonacci(n - 1) and fibonacci(n - 2) to get the sum.
4fill in blank
hardFill both blanks to create a recursive function that sums numbers from n down to 1.
DSA Python
def sum_to_n(n): if n == 0: return 0 else: return n [1] sum_to_n(n [2] 1)
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication instead of addition.
Not reducing n in the recursive call.
✗ Incorrect
The function adds n to the sum of numbers from n - 1 down to 1.
5fill in blank
hardFill all three blanks to create a recursive function that reverses a string.
DSA Python
def reverse_string(s): if s == "": return "" else: return s[[1]] + reverse_string(s[[2]:[3]])
Attempts:
3 left
💡 Hint
Common Mistakes
Using positive index instead of negative for last character.
Incorrect slicing range causing errors.
✗ Incorrect
The function takes the last character s[-1] and adds the reverse of the string except the last character s[0:-1].
