Which of the following statements are true?
Practice Recursion Questions – Which of the following statements are true?
Practice Recursion Questions – Which of the following statements are true?
Practice Recursion Questions – Fill in the code to complete the following function for computing factorial.
def factorial(n):
if n == 0: # Base case
return1
else:
return _____________________ # Recursive call
Practice Recursion Questions – Analyze the following recursive function.
def factorial(n):
return n * factorial(n – 1)
Practice Recursion Questions – Fill in the code to complete the following function for sorting a list.
def sort(lst):
_________________________ # Sort the entire list
def sortHelper(lst, low, high):
if low < high:
# Find the smallest number and its index in lst[low .. high]
indexOfMin = low
min = lst[low]
for i in range(low + 1, high + 1):
if lst[i] < min:
min = lst[i]
indexOfMin = i
# Swap the smallest in list(low .. high) with list(low)
lst[indexOfMin] = lst[low]
lst[low] = min
# Sort the remaining list(low+1 .. high)
sortHelper(lst, low + 1, high)
Practice Recursion Questions – Show the output of the following code:
def f2(n, result):
if n == 0:
return0
else:
return f2(n – 1, n + result)
print(f2(2, 0))
Practice Recursion Questions – In the following function, what is the base case?
def xfunction(n):
if n == 1:
return1
else
return n + xfunction(n – 1)