Exercise
For each of the following tasks, draw what the final list looks like and diagram how you
would solve the problem by writing a step by step process to solve the task.
1. Given a list of integers, replace every element at an even index with 0. Remember lists
start at index 0, so the first element should be replaced and then every other element
thereafter.
Initial list:
3
4
1
2
89
Final list:
Solution Steps:

Exercise For each of the following tasks draw what the final list looks like and diagram how you would solve the problem by writing a step by step process to s class=

Respuesta :

Answer:

[0,4,0,2,0]

Explanation:

You need to iterate through the list and check if the index is even by comparing the rest of its division by 2 with 0. This can be achieved using the following algorithm (written in Python, so it can easily be translated to other languages)

mylist = [3,4,1,2,89]

for i in range(0, len(mylist)): #For each index of the list

   if i%2==0: #If the rest of the division of i by 2 is 0, which means it is even

       mylist[i]=0 #Assigns zero to current index