Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
itertools.groupby() solution in Clear category for Sum Consecutives by kurosawa4434
from itertools import groupby
def sum_consenutives(a):
return [k*len(list(v)) for k, v in groupby(a)]
if __name__ == '__main__':
print("Example:")
print(sum_consenutives([1, 1, 1, 1]))
# These "asserts" are used for self-checking and not for an auto-testing
assert sum_consenutives([1, 1, 2, 1]) == [2, 2, 1]
assert sum_consenutives([1, 1, 1, 1]) == [4]
assert sum_consenutives([1, 1, 2, 2]) == [2, 4]
assert sum_consenutives([3, 3, 3, 4, 4, 5, 6, 6]) == [9, 8, 5, 12]
assert sum_consenutives([1]) == [1]
assert sum_consenutives([]) == []
print("Coding complete? Click 'Check' to earn cool rewards!")
June 8, 2019