DEV Community

Discussion on: Daily Challenge #206 - Pound Means Backspace

Collapse
 
jhermann profile image
Jürgen Hermann • Edited

Python – No doc strings etc., it's a challenge after all…

import re

def clean_text(text, _back_hash = re.compile(r'[^#]#')):
    changes = '#' in text
    while changes:
        text, changes = _back_hash.subn('', text)
    return text.replace('#', '')


def tests():
    data = (
        ("abc#def##ghi###", "abd"),
        ("abc#d##c", "ac"),
        ("abc##d######", ""),
        ("#######", ""),
        ("", ""),
    )
    for inp, out in data:
        assert clean_text(inp) == out, \
               f"{inp!r} cleaned is not {out!r} but {clean_text(inp)!r}"
    print('All tests OK!')


if __name__ == '__main__':
    for x in ('abc#de##c', 'abc####dhh##c#', 'Thee# Empires# SS#tateBuildingg#'):
        print(f"{x!r} ⇒ {clean_text(x)!r}")
    tests()
Collapse
 
aminnairi profile image
Amin

This is always a good idea to comment your code. Beginners trying to solve these challenges can learn a lot from veterans like you!