Validating Roman Numerals in Python | HackerRank Solution

Hello coders, today we are going to solve Validating Roman Numerals HackerRank Solution in Python.

Validating Roman Numerals

Task

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

Solution – Validating Roman Numerals in Python

regex_pattern = r'M{0,3}(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[VX]|V?I{0,3})$'    # Do not delete 'r'.

import re
print(str(bool(re.match(regex_pattern, input()))))

Disclaimer: The above Problem (Validating Roman Numerals) is generated by Hacker Rank but the Solutions is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *