Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
weighted_sum solution in Clear category for Can Balance by flpo
from typing import Iterable
def weighted_sum(side):
return sum(i*x for i, x in enumerate(side, 1))
def can_balance(weights: Iterable) -> int:
def is_balanced(i):
return weighted_sum(weights[:i][::-1]) == weighted_sum(weights[i+1:])
return next(filter(is_balanced, range(len(weights))), -1)
Dec. 13, 2018