I need to write a program which would take an alphanumeric string as input. The string would contain only lower case characters and number from 0 to 9.
I have to compress the alphabets as alphabets multiplied by number of times continous repetition, like:
aaa
toa3
bbbb
tob4
c
toc1
If there are any integers in the input string, then I have to add the integers.
Return the compressed string multiplied by added integers.
def std(string):
res =" "
con =1
res += string[0]
for i in range((len(string)-1)):
if (string[i] == string[i+1]):
con+=1
else:
if (con>1):
res += str(con)
res+=string[i+1]
con =1
if (con>1):
res+= str(con)
return res
print(std('aabbb3cccc2d'))
I wrote this program and got the output a2b
.
The expected output is a2b3c4d1a2b3c4d1a2b3c4d1a2b3c4d1a2b3c4d1
. Could anyone tell me where am I going wrong?