Transform "4CA2CACA" to "CCCCACCACA"
Question:
I have already tried this (as somebody told me on another question):
import re
def decode(txt):
list = []
for cnt, char in re.findall(r"([d*])([^d])", txt):
list.extend((char * (int(cnt) if cnt else 1)))
list = "".join(list)
return list
Example:
print(decode("2CACA2CACA3CACACA3CAC"))
This is what I get
CCCCCCCCCC
And this is what I need
CCACACCACACCCACACACCCAC
Answers:
What you are missing is characters without a digit in front. This will include those:
import re
def decode(txt):
_list = []
for cnt, char, single_char in re.findall(r"(d)([^d])|([^d])", txt):
if single_char:
_list.extend(single_char)
else:
_list.extend((char * (int(cnt) if cnt else 1)))
_list = "".join(_list)
return _list
print(decode("2CACA2CACA3CACACA3CAC"))
If you want to do it without any imports, then you could do something like this:
x = '2CACA2CACA3CACACA3CAC0M'
int_string = ""
char_list = []
for char in x:
if char.isnumeric():
int_string += char
continue
else:
if not int_string:
char_list.append(char)
else:
char_list.append(char * int(int_string))
int_string = ""
print("".join(char_list))
This will work for any positive integers, even zero, as you can see in the above example.
re.sub
can take a named function or lambda as its second argument, and you can use this to accomplish your goal. Using this approach you simply don’t do any substitution when a letter does not have a number in front of it.
def decode(s):
return re.sub(r'(d+)([a-zA-Z])',
lambda m: m.group(2)*int(m.group(1)),
s)
decode("2CACA2CACA3CACACA3CAC")
# 'CCACACCACACCCACACACCCAC'
I have already tried this (as somebody told me on another question):
import re
def decode(txt):
list = []
for cnt, char in re.findall(r"([d*])([^d])", txt):
list.extend((char * (int(cnt) if cnt else 1)))
list = "".join(list)
return list
Example:
print(decode("2CACA2CACA3CACACA3CAC"))
This is what I get
CCCCCCCCCC
And this is what I need
CCACACCACACCCACACACCCAC
What you are missing is characters without a digit in front. This will include those:
import re
def decode(txt):
_list = []
for cnt, char, single_char in re.findall(r"(d)([^d])|([^d])", txt):
if single_char:
_list.extend(single_char)
else:
_list.extend((char * (int(cnt) if cnt else 1)))
_list = "".join(_list)
return _list
print(decode("2CACA2CACA3CACACA3CAC"))
If you want to do it without any imports, then you could do something like this:
x = '2CACA2CACA3CACACA3CAC0M'
int_string = ""
char_list = []
for char in x:
if char.isnumeric():
int_string += char
continue
else:
if not int_string:
char_list.append(char)
else:
char_list.append(char * int(int_string))
int_string = ""
print("".join(char_list))
This will work for any positive integers, even zero, as you can see in the above example.
re.sub
can take a named function or lambda as its second argument, and you can use this to accomplish your goal. Using this approach you simply don’t do any substitution when a letter does not have a number in front of it.
def decode(s):
return re.sub(r'(d+)([a-zA-Z])',
lambda m: m.group(2)*int(m.group(1)),
s)
decode("2CACA2CACA3CACACA3CAC")
# 'CCACACCACACCCACACACCCAC'