You thought summation was easy, until you accidentally time-traveled to ancient Rome and you have to deal with arithmetic such as
Fortunately, you brought a roman numeral cheatsheet.
I | V | X | L | C | D | M |
1 | 5 | 10 | 50 | 100 | 500 | 1000 |
This file contains 100 space-separated roman numerals. What is the sum of all these numbers written in decimal representation?
Notes :
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
Since we are not dealing with Subtractive Notation, we just need to map the roman numerals to decimals as presented in the cheatsheet above and sum them up.
Unlike
list
where the indexes are non-negative integers, dictionary let us define our own indexes. Literally,m['I'] = 1
and so on. However, note that there's a map of space to 0 at the end of the dictionary. This is because the input are space-separated and we want to ignore those.This is a very good use of Python dictionary. It reduces tedious
if-else
statement and provide more readability.