a pythonic way of doubling consecutive binary bits [closed]

How’s that?

from itertools import groupby
input_list =  [0, 1, 0, 0, 1, 1, 1, 0, 1, 0]
results = [0, 0]  # First value is zeros, second is ones
for key, values in groupby(input_list):
    results[key] += (2**len(tuple(values))) - 1
assert results == [6,9]

Leave a Comment