-1

I am trying to solve a programming question to convert a string to below form:
input: aaaabbbcc
output: a4b3c2

My code is like below:

def encode(s):
    output = []
    i = 0
    j = 1
    while i < len(s) and j < len(s)-1 :
        count = 1
        output.append(s[j])


    while s[i] == s[j] :
        count += 1
        j+=1
        i+=1

    output.append(count)
    i += 1
    j += 1


new_s = "".join(str(x) for x in output)
return new_s

But I am getting the below exception :
Traceback (most recent call last):

File "encode.py", line 30, in
print encode(s)
File "encode.py", line 13, in encode
while s[i] == s[j] :
IndexError: string index out of range

I am not able to understand the bug here. Can someone please help me?

Dovydas Šopa
  • 2,282
  • 8
  • 26
  • 34
Agniswar Bakshi
  • 328
  • 5
  • 21

5 Answers5

4

You could use groupby function:

import itertools
result = ""
for k, group in itertools.groupby('aaaabbbcc'):
    result += '%s%d' % (k, len(list(group)))
print(result)
>>> a4b3c2
Maurice Meyer
  • 17,279
  • 4
  • 30
  • 47
2

As others have indicated you are not checking the list bounds in the inner loop.

Note that you can do the string conversion with a regular expression (import re) and list comprehension, like this:

''.join([ch + str(len(m)) for m, ch in re.findall(r"((.)\2*)", "aaaabbbcc")])
trincot
  • 317,000
  • 35
  • 244
  • 286
1

Your code is working fine.The only issue being if string has a single letter like aaabbd d1 will not come back. You can try with re as well.

x="aaaabbbccd"
print "".join([j+str(len(i)) for i, j in re.findall(r"((.)\2*)", x)])
vks
  • 67,027
  • 10
  • 91
  • 124
1

You could use collections Counter

from collections import Counter

in_str = "aaaabbbccd"
out_str = ""
letters = Counter(in_str)

for l in letters:
    out_str += l + str(letters[l])

print(out_str) # a4b3c2d1
# Note: in_str of "aabaa" will produce out_str of "a4b1"
dǝɥɔS ʇoıןןƎ
  • 1,674
  • 5
  • 19
  • 42
0

You can convert your string into set. Than you can iterate the set and call the count() for finding number of repetition character.

input_str = 'aaaabbbcc'
# converting into set
input_set=set(list(input_str))
for i in input_set:
    print(i+str(input_str.count(i)),end='')
# as set is unordered so output will come unordered.
R.A.Munna
  • 1,699
  • 1
  • 15
  • 29