Answer to Question #347483 in Python for Ching

Question #347483

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


if I insert any element in the range of (1 to 10^5) between any two items of the array then it will give some other value, like this I have to give the minimum cost possible by inserting a number.



1
Expert's answer
2022-06-06T08:58:13-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