Er, ok, since you put up code, here is what I was alluding to in my first reply. A for loop, a placeholder and a counter...
PHP Code:
def letters(word=''):
count = 1 # Counter
prev = '' # Placeholder
for c in word: # For loop
if c != prev:
if prev: # Prevents printing the empty prev.
print prev, count
count = 1
prev = c
elif c == prev:
count = count + 1
else: # needed to print out the final letter's count
print c, count
while 1:
letters(raw_input(': '))
Results in the following.
Code:
{grey@igbuntu:~} python foo.py
: mississippi
m 1
i 1
s 2
i 1
s 2
i 1
p 2
i 1
: ragoo
r 1
a 1
g 1
o 2
: Gooooooooooooooaaaaaaaallllll!!
G 1
o 14
a 8
l 6
! 2
Since that accurately counts all letters it is simply a matter of adjusting the exact display logic with a few ifs. Or, if you're getting really fancy, throwing it all into a dict of lists and extracting the information from that dict as needed.
Bookmarks