Validating Roman Numerals in Python HackerRank Solution

Hello Programmers, In this post, you will know how to solve the Validating Roman Numerals in Python HackerRank Solution. This problem is a part of the HackerRank Python Programming Series.

Ezoicreport this adValidating Roman Numerals in Python HackerRank Solution
Validating Roman Numerals in Python HackerRank Solutions

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

Validating Roman Numerals in Python HackerRank Solutions

Ezoicreport this adproblem

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.

Input Format :

A single line of input containing a string of Roman characters.

Output Format :

Output a single line containing True or False according to the instructions above.

Constraints :

The number will be between 1 and 3999(both included).

Sample Input :

CDXXI

Sample Output :

True

References :

Regular expressions are a key concept in any programming language. A quick explanation with Python examples is available here. You could also go through the link below to read more about regular expressions in Python.
https://developers.google.com/edu/python/regular-expressions

Validating Roman Numerals in Python HackerRank Solutions

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'.
# Validating Roman Numerals in Python - Hacker Rank Solution END
import re
print(str(bool(re.match(regex_pattern, input()))))

Disclaimer: The above Problem (Validating Roman Numerals in Python) is generated by Hackerrank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: Validating phone number in Python HackerRank Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad