Test Your Knowledge 4

Let's take a small quiz!

We'll cover the following

Quiz on Recursion with Strings

This Quiz will take maximum 10 minutes.

1

The code below replaces all occurrences of substring b with substring a in string using recursion:

def replace(string, a, b) :
  # Base case
  if not string:
    return ""
  
  # Recursive case
  ______________________________________________

What should be the recursive case of the following code?

A)
  elif string[:len(b)] == b :
    return a + replace(string[len(b):], a, b)
    
  else :
    return string[0] + replace(string[1:], a, b)
B)
  else :
    return string[0] + replace(string[1:], a, b)
C)
replace(string, a - 1, b - 1)
D)
  elif string == b :
    return replace(string[len(b):], a, b)
    
  else :
    return string[0] + replace(string[1:], a, b)
Question 1 of 50 attempted

In the next chapter, we will be learning how to deal with arrays using recursion.