Answer to Question #347474 in Python for Tyu

Question #347474

Minimize the cost of an array where


cost = summation of square of difference of two consecutive array elements.


ex:


arr = [1,2,3,4]


sum = (1-2)^2 + (2-3)^2 + (3-4)^2


Output = 3



1
Expert's answer
2022-06-06T08:32:01-0400
entered_list = input("Enter a list, separating numbers with spaces: ").split()

test_list = list(map(int, entered_list))

cost = [((a - b) ** 2) for a, b in zip(test_list, test_list[1:])]

print("Cost: ", sum(cost))

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