Skip to content Skip to sidebar Skip to footer

Encode Repeated Letters In A String With Number

A string 'abc' must become 'a1b1c1'. String 'aaabcca' - 'a3b1c2a1' I wrote python function, but it fails to add the last letter and 'abc' is only 'a1b1'. string = 'aaabbcc' coded =

Solution 1:

Use itertools.groupby.

>>> from itertools import groupby
>>> s = 'aaabcca'
>>> ''.join('{}{}'.format(c, sum(1 for _ in g)) for c, g in groupby(s))
'a3b1c2a1'

Details on what groupby produces:

>>> groups = groupby(s)
>>> [(char, list(group)) for char, group in groups]
[('a', ['a', 'a', 'a']), ('b', ['b']), ('c', ['c', 'c']), ('a', ['a'])]

Solution 2:

Some regex magic:

import re

s = 'aaawbbbccddddd'
counts = re.sub(r'(.)\1*', lambda m: m.group(1) + str(len(m.group())), s)
print(counts)

The output:

a3w1b3c2d5

Details:

regex pattern:

  • (.) - capturing a character .(any char) into the 1st captured group
  • \1* - matches zero or more consecutive \1 which is a reference to the 1st captured group value (matching a potentially sequence of the same character)

replacement:

  • m.group(1) - contains the 1st matched group value
  • str(len(m.group())) - get length of the entire character sequence matched

Solution 3:

You forget to explicitly add the very last iteration.

string = "aaabb"
coded = ''
if len(string) == 0:
   print('')
else:
  count = 1   #start with the first char, not zero!
  prev = string[0]
  for i in range(1,len(string)):
    current = string[i]
    if current == prev:     
       count +=1
    else:              
      coded += prev
      coded += str(count)
      count = 1
      prev = current
coded += prev       # these two
coded += str(count) # lines

print(coded)

I would prefer a less complicated loop, though:

string = "aaabbcc"
coded = ''
while string:
    i = 0
    while i < len(string) and string[0] == string[i]:
        i += 1
    coded += string[0]+str(i)
    string = string[i:]

print(coded)

Solution 4:

If you wonder why your code didn't work or you don't want to use any external libraries here's working version of your code

string = "aaabbcc"
coded = ''

if len(string) == 0:
   print('')

else:
  count = 0
  prev = string[0]
  for i in range(1,len(string)):
    current = string[i]
    count +=1

    if current != prev:
      coded += prev
      coded += str(count)
      count = 0

    prev = current

  coded += current
  coded += str(count+1)

print(coded) # -> a3b2c2

Post a Comment for "Encode Repeated Letters In A String With Number"