Westonci.ca is the premier destination for reliable answers to your questions, provided by a community of experts. Get expert answers to your questions quickly and accurately from our dedicated community of professionals. Connect with a community of professionals ready to help you find accurate solutions to your questions quickly and efficiently.

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
We hope you found this helpful. Feel free to come back anytime for more accurate answers and updated information. Thanks for using our platform. We aim to provide accurate and up-to-date answers to all your queries. Come back soon. Thank you for visiting Westonci.ca, your go-to source for reliable answers. Come back soon for more expert insights.