Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First simple while cycle solution in Clear category for Sum Consecutives by Molot
def sum_consecutives(seq):
if not seq:
return []
curr = seq.pop(0)
res = [curr]
while seq:
if curr == seq[0]:
res[-1] += seq.pop(0)
else:
curr = seq.pop(0)
res.append(curr)
return res
if __name__ == '__main__':
print("Example:")
print(list(sum_consecutives([1, 1, 1, 1])))
# These "asserts" are used for self-checking and not for an auto-testing
assert list(sum_consecutives([1, 1, 1, 1])) == [4]
assert list(sum_consecutives([1, 1, 2, 2])) == [2, 4]
assert list(sum_consecutives([1, 1, 2, 1])) == [2, 2, 1]
assert list(sum_consecutives([3, 3, 3, 4, 4, 5, 6, 6])) == [9, 8, 5, 12]
assert list(sum_consecutives([1])) == [1]
assert list(sum_consecutives([])) == []
print("Coding complete? Click 'Check' to earn cool rewards!")
July 6, 2019
Comments: