Click here to Skip to main content
15,902,447 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
An list of given length is given

input array- 4 3 1 2 0
indexes- 0 1 2 3 4

expected o/p- 0 4 9 1 16
my output- 16 4 9 1 0

Ouput Explanation- at 0th index value is 4 so we have to check the value at index 4 and square it which means 0x0=0 , and place the value at 0th index in same array

What I have tried:

Below is what I have tried.Please suggest improvements.
x=[]
for i in range(len(a)):
    x.append(a.index(i)**2)
print(x)
Posted
Updated 11-Feb-21 10:04am
Comments
Richard MacCutchan 12-Feb-21 3:44am    
Use the insert method to add each square in front of the previous entry.

1 solution

Read what you wrote:
Ouput Explanation- at 0th index value is 4 so we have to check the value at index 4 and square it which means 0x0=0 , and place the value at 0th index in same array

So do that: get the value at the loop variable index, and use that value as an index into the array - then square that value and store back in the array using the loop variable as an index again.

All your code does is get the value at the look variable index, square it, and store that in a different array.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900