I am receiving string in the form of a3b4x2
which needs to be decompressed to get aaabbbbxx
.If the digits are fixed, such as this example, it is easy to decompress. However, the input can come in the form of a1192x12
which needs to decompressed to get aaaaaaaaaaa99xxxxxxxxxxxx
or in the form of 31142
to get 3333333333344
. What is the most efficient way to do this?
If you consider the number of occurence of a letter in one or two digits (not more),
you can use the following regular expression to find couples of the form lettre + number:
r"(\w)(\d{1,2})"
.
Here is a possible solution:
import re
samples = ["a3b4x2", "a1192x12", "31142"]
for sample in samples:
result = ""
for couple in re.findall(r"(\w)(\d{1,2})", sample):
letter, number = couple
number = int(number)
result += letter * number
print(result)
You get:
aaabbbbxx
aaaaaaaaaaa99xxxxxxxxxxxx
3333333333344