Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
You are given a string, and you have to validate whether it’s a valid Roman numeral. If it is valid, print True. Otherwise, print False. Try to create a regular expression for a valid Roman numeral.
A single line of input containing a string of Roman characters.
Output a single line containing True or False according to the instructions above.
The number will be between 1 and 3999(both included).
CDXXI
True
import sys
import re
pattern = '^M{0,3}(D?C{0,3}|C(D|M))(L?X{0,3}|X(L|C))(V?I{0,3}|I(V|X))$'
string = sys.stdin.readline().strip()
match = re.search(pattern, string)
if match and string != '':
print 'True'
else:
print 'False'
thousand = 'M{0,3}'
hundred = '(C[MD]|D?C{0,3})'
ten = '(X[CL]|L?X{0,3})'
digit = '(I[VX]|V?I{0,3})'
regex_pattern = r"%s%s%s%s$" % (thousand, hundred, ten, digit) # Do not delete 'r'
# Enter your code here. Read input from STDIN. Print output to STDOUT
import re
pattern = '^(?=[MDCLXVI])M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$'
roman = str(raw_input())
if re.search(pattern, roman):
print 'True'
else:
print 'False'
# Enter your code here. Read input from STDIN. Print output to STDOUT
import re
ptrn = r'^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$'
s = input().strip()
x = re.match(ptrn,s)
if x:
print(True)
else:
print(False)
In our experience, we suggest you solve this Validating Roman Numerals Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
Validating Roman Numerals is available on Hacker Rank for Free, if you are stuck anywhere between compilation, just visit Queslers to get all Hacker Rank Solution
I hope this Validating Roman Numerals Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Hacker Rank Problem & Solutions >>
Mini-Max Sum Hacker Rank Solution
String Validators Hacker Rank Solution
Text Alignment Hacker Rank solution