• + 1 comment

    I didn't like this challenge. I wasn't sure how to approach this at first, then decided I should do it by base-10 number place values. I started with the ones and built up from there. After I thought the regex was about 80% complete, I submitted it to the HackerRank tester. It found a couple of problems, which took a few minutes more to fix.

    The regex is not exhaustive, but it passed all the HackerRank tests. I see others have posted more concise ones here, but are they exhaustive? Clearly, I have more to learn about regexes.

    regex_pattern = r"^(M{,3}(?=([^M]))){,1}(C{,1}M{,1}){,1}(C{,1}D{,3}){,1}(X{,1}C{,3}){,1}(X{,1}L{,1}){,1}(I{,1}X{,3}){,1}(I{1,3}[VX]{,1}|VI{,3}){,1}$"
    
    import re
    print(str(bool(re.match(regex_pattern, input()))))