Answer to Question #167980 in Python for P

Question #167980

Square at Alternate Indices

Given an array

myArray of numbers, write a function to square the alternate numbers of the myArray, starting from index 0.Input

  • The input will be a single line containing an array myArray

Output

  • The output should be a single line containing an array with alternate numbers squared

Constraints

  • Each value in the array must be a number

input:

[1,2,3,4,5]

output:

[1,2,9,4,25]


input:

[2,4]

output:

[4,4]



1
Expert's answer
2021-03-02T08:03:35-0500
list1 = [1, 2, 3, 4, 5]

def fun_square_odd(input_array):
    """Squares the odd input of given array"""

    for item in list1:
        if list1.index(item) % 2 == 0:
            ind = list1.index(item)
            list1[ind] = item * item
    print(list1)

fun_square_odd(list1)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS