Does anyone know how to decompress the RLE algorithm but the input must start with a letter for example "A2B5". The input from my program starts with a number, for example "2B5B".
Asked
Active
Viewed 223 times
0
-
1[Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Muhammad Mohsin Khan Mar 14 '22 at 15:20
1 Answers
0
Here is an implementation of the algorithm assuming only single letters are to be replicated and multiple digits are to be supported.
compressed = "A10B5"
def is_digit(s):
try:
int(s)
return True
except:
return False
def decompress(c):
result = ""
ch = ''
count = 0
for i in c:
if is_digit(i):
count = count * 10 + int(i)
else:
result += ch * count
count = 0
ch = i
result += ch * count
return result
print(decompress(compressed))

mmmm
- 16
- 4
-
Well, there goes the whole "stackoverflow is not a code-writing service" thing. – Mark Adler Mar 14 '22 at 17:58