Question: Complete the following function using recursion: def replace(thelist,a,b): Returns: a COPY of thelist but with all occurrences of int a replaced by int b. Does

Complete the following function using recursion:

def replace(thelist,a,b): """Returns: a COPY of thelist but with all occurrences of int a replaced by int b. Does not change thelist.

Example: replace([1,2,3,1], 1, 4) = [4,2,3,4].

Precondition: thelist is a possibly empty list of ints."""

You may use the following test cases to verify your answer:

test_cases = { ((5, 6), 5, 4): [4, 6], ((5, 6), 6, 4): [5, 4], ((5, 5), 5, -4): [-4, -4], ((), 1, 2): [], ((5, 3, 3455, 74, 74, 74, 3), 3, 20): [5, 20, 3455, 74, 74, 74, 20], ((5, 3, 3455, 74, 74, 74, 3), 1, 20): [5, 3, 3455, 74, 74, 74, 3], }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!