Test Your Knowledge 4
Let's take a small quiz!
We'll cover the following...
...
This Quiz will take approximately 10 minutes.
1
The code below replaces all occurrences of substring b
with substring a
in string
. This process is done recursively.
def replace(string, a, b) :
# Base case
if not string:
return ""
# Recursive case
______________________________________________
What should the recursive case of the following code be?
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
...
Access this course and 1400+ top-rated courses and projects.