Skip to content
Advertisement

Count consecutive characters

How would I count consecutive characters in Python to see the number of times each unique digit repeats before the next unique digit?

At first, I thought I could do something like:

JavaScript

So that in this manner I could see the number of times each unique digit repeats. But this, of course, falls out of range when i reaches the last value.

In the example above, I would want Python to tell me that 1 repeats 1, and that 0 repeats 3 times. The code above fails, however, because of my while statement.

How could I do this with just built-in functions?

Advertisement

Answer

A solution “that way”, with only basic statements:

JavaScript

Output :

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement