Problem 129155 · hard · Phase 01 Prerequisites & Setup

Checking the Cornerstone Dates

strings · loops · conditionals · functions

A city archive is typing up the dates carved on old cornerstones, and many transcriptions are wrong. Write cornerstone_year(s) that returns the year written by the Roman numeral s, or -1 if s is not written in standard form.

The symbols are I=1, V=5, X=10, L=50, C=100, D=500, M=1000. Standard form writes the thousands, hundreds, tens and units in that order, each with exactly one of these spellings (or nothing when that digit is 0):

units:     I II III IV V VI VII VIII IX
tens:      X XX XXX XL L LX LXX LXXX XC
hundreds:  C CC CCC CD D DC DCC DCCC CM
thousands: M MM MMM

So only years 1 to 3999 exist. Anything else returns -1: lower-case letters, other characters, the empty string, IIII, IC, VX, MMMM, XIX written as XVIIII, and so on.

Examples

Input:  s = "MCMXCIV"
Output: 1994

Input:  s = "IC"
Output: -1
Explanation: 99 is written XCIX.

Input:  s = "XLIX"
Output: 49

Constraints

  • 0 <= len(s) <= 30
  • s may contain any printable characters.

Goals

  • Read a Roman numeral with the subtraction rule
  • Write the reverse conversion as a second function
  • Validate an input by converting it there and back
Starting Python…