We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Validating Roman Numerals
Validating Roman Numerals
Sort by
recency
|
205 Discussions
|
Please Login in order to post a 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.
Here is HackerRank Validating Roman Numerals in Python solution - https://programmingoneonone.com/hackerrank-validating-roman-numerals-solution-in-python.html
Interesting problem, and I learned a fine point or two about Roman numerals. :)
My solution is a little different in that I didn't use any | alternatives:
regex_pattern = r"^M{0,3}(CM)?(C?D)?C{0,3}(XC)?(X?L)?X{0,3}(IX)?(I?V)?I{0,3}$"
regex_pattern = r"^(M{0,3})(C{0,3}|CD|D|DC|DCC|DCCC|CM)(X{0,3}|XL|L|LX|LXX|LXXX|XC)(I{0,3}|IV|V|VI|VII|VIII|IX)$"
import re print(str(bool(re.match(regex_pattern, input()))))