Search⌘ K

A Pleasing Symmetry

Explore how to create and test functions that convert integers to Roman numerals and vice versa. Understand validation with regular expressions and develop unit tests to ensure your conversion functions are accurate and reliable, covering all valid inputs between 1 and 3999.

We'll cover the following...

Converting a string from a Roman numeral to an integer sounds more difficult than converting an integer to a Roman numeral. Certainly there is the issue of validation. It’s easy to check if an integer is greater than 0, but a bit harder to check whether a string is a valid Roman numeral. But we already constructed a regular expression to check for Roman numerals, so that part is done.

That leaves the problem of converting the string itself. As we’ll see in a minute, thanks to the rich data structure we defined to map individual Roman numerals to integer values, the nitty-gritty of the from_roman() function is as straightforward as the to_roman() function. ...