Welcome to Westonci.ca, the place where your questions find answers from a community of knowledgeable experts. Experience the convenience of finding accurate answers to your questions from knowledgeable experts on our platform. Discover in-depth answers to your questions from a wide network of professionals on our user-friendly Q&A platform.

10. Write a function called replace_elem that takes an array and two values and uses the simple search
algorithm to replace all instances of the first value with the second value
Sample Run 1:
a = [7, 4, 10, 3, 7, 2, 4, 5]
print (replace_elem(a, 4, 6))
Should output:
[7, 6, 10, 3, 7, 2, 6, 5]
Sample Run 2
a = [7, 3, 10, 3, 7, 2, 9, 5]
print (replace elem(a, 4, 6))
Should output
[7, 3, 10, 3, 7, 2, 9, 5]

Sagot :

The function that replaces the values in an array is as follows:

def replace_elem(a, integer1, integer2):

    for i in range(len(a)):

         if a[i] == integer1:

              a[i] = integer2

    return a

print(replace_elem([7, 4, 10, 3, 7, 2, 4, 5], 4, 6))

Code explanation.

The code is written in python.

  • We defined a function named "replace_elem". The function accepts an array a, and integers integer1 and integer2.
  • Then, we used a loop to loop the index of the array.
  • If any of the index value is equals to the integer1, we replace it with integer2.
  • Then return the new values of the array a.
  • Finally, we call the function with its parameters.

learn more on function here: https://brainly.com/question/15691123

View image vintechnology