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.
The regex looks a bit hard, but in fact, if you understood the first pattern, you can copy and paste until "I".
The hard thing of this exercice is to implement the "substraction roman number" (IX, CD, XL etc...).
Firstly, we know that at the start, it's possible to have 0 to 3 "M" (for 3000+)
So : M{0,3}
Then, this is the most important :
Below "M" we can have : "CM" (=900) or "CD" (=400) or "D" (=500), or "C" (0 to 3)(=100-300)
SO we have to handle these possibilities with "|" (or) :
(CM|CD|D?C{0,3})
The "pattern" is : (biggest_substraction-number|lowest_substraction_number|biggest_digit?lowest|digit{0,3})
It's mean : We can have "CM" or "CD" or "D" else we have 0 to 3 times "C".
It's maybe hard to follow, but I just want to precise that the logic can be repeated after because the pattern is same.
So for number between 99 to 10 we can have : "XC" (=90) or "XL" (=40) or "L" (=50) or "X" (0 to 3)(=10-30)
Who is the bigger substraction number here ? This is "XC", so : (XC|??|???{0,3})
Who is the lowest substraction number here ? (Obviously, the one remains) This is XL, so : (XC|XL|???{0,3})
Who is the biggest roman digit here (between L and X) ? This is "L", so : (XC|XL|L??{0,3})
Finally, who is the lowest roman digit here ? (Obviously, the one remains) This is "X", so : (XC|XL|L?X{0,3})
That's it !
Now you have : M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3}))
And you have to repeat this step for IX, IV, V and I.
I hope I not confused a lot of people. I have understand the exercise after understand this logic lmaooo, so I wanted to share with other people in case it's useful for some of you.
Here the code :
regex_pattern=r"^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$"# Do not delete 'r'.importreprint(str(bool(re.match(regex_pattern,input()))))
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Validating Roman Numerals
You are viewing a single comment's thread. Return to all comments →
The regex looks a bit hard, but in fact, if you understood the first pattern, you can copy and paste until "I".
The hard thing of this exercice is to implement the "substraction roman number" (IX, CD, XL etc...).
Firstly, we know that at the start, it's possible to have 0 to 3 "M" (for 3000+) So : M{0,3}
Then, this is the most important :
Below "M" we can have : "CM" (=900) or "CD" (=400) or "D" (=500), or "C" (0 to 3)(=100-300)
SO we have to handle these possibilities with "|" (or) :
(CM|CD|D?C{0,3})
The "pattern" is : (biggest_substraction-number|lowest_substraction_number|biggest_digit?lowest|digit{0,3})
It's mean : We can have "CM" or "CD" or "D" else we have 0 to 3 times "C".
It's maybe hard to follow, but I just want to precise that the logic can be repeated after because the pattern is same.
So for number between 99 to 10 we can have : "XC" (=90) or "XL" (=40) or "L" (=50) or "X" (0 to 3)(=10-30)
Who is the bigger substraction number here ? This is "XC", so : (XC|??|???{0,3})
Who is the lowest substraction number here ? (Obviously, the one remains) This is XL, so : (XC|XL|???{0,3})
Who is the biggest roman digit here (between L and X) ? This is "L", so : (XC|XL|L??{0,3})
Finally, who is the lowest roman digit here ? (Obviously, the one remains) This is "X", so : (XC|XL|L?X{0,3})
That's it !
Now you have : M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3}))
And you have to repeat this step for IX, IV, V and I.
I hope I not confused a lot of people. I have understand the exercise after understand this logic lmaooo, so I wanted to share with other people in case it's useful for some of you.
Here the code :