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
|
204 Discussions
|
Please Login in order to post a comment
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()))))
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()))))