Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Transform "4CA2CACA" to "CCCCACCACA"

I have already tried this (as sombody 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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

CCCCCCCCCC

And this is what I need

CCACACCACACCCACACACCCAC

>Solution :

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"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading