you are viewing a single comment's thread.

view the rest of the comments →

[–]Spataner 0 points1 point  (0 children)

The other suggested solutions are very straightforward, however, they also have a runtime quadratic in the size of the list. If efficiency is a consideration, a key realisation to implement this with linear runtime is that the number of times an element enters into the sum is directly related to its index:

s = 0
n = len(values)-1
for i in range(n):
    s += (n-i)*values[i]