3 ms·
Yes, it does solve it. It produces the expected output (`a` should occur twice) Here's solution that doesn't use itertools.groupby() https://docs.python.org/3/
by d0mine 6y ago
Yes, it does solve it. It produces the expected output (`a` should occur twice)
Here's solution that doesn't use itertools.groupby()
https://docs.python.org/3/library/itertools.html?highlight=itertools#itertools.groupby https://docs.python.org/3/library/itertools.html?highlight=i...
:
result = []
prev, count = None, 0
for c in s+"\0": # note: assuming "\0" can't be in `s`
if c != prev: # starting new sequence
if count: # save count for old sequence
result.append((prev, count))
prev, count = c, 1 # reset count
else: # continuation of old sequence
count += 1
- chrisandchris 6y agoOk, thanks. I don‘t speak enough python to understand that it solves the issue.