# Roman Numberals Decoder
"""
Description:

Create a function that takes a Roman numeral as its argument and returns its
value as a numeric decimal integer. You don't need to validate the form of the
Roman numeral.

Modern Roman numerals are written by expressing each decimal digit of the
number to be encoded separately, starting with the leftmost digit and skipping
any 0s. So 1990 is rendered "MCMXC" (1000 = M, 900 = CM, 90 = XC) and 2008 is
rendered "MMVIII" (2000 = MM, 8 = VIII). The Roman numeral for 1666, "MDCLXVI",
uses each letter in descending order.

Example:

solution('XXI') # should return 21
C# RomanDecode.Solution("XXI") -- should return 21

Courtesy of rosettacode.org
"""


def solution(roman):
    dict = {
        "M": 1000,
        "D": 500,
        "C": 100,
        "L": 50,
        "X": 10,
        "V": 5,
        "I": 1
    }

    last, total = 0, 0
    for c in list(roman)[::-1]:
        if last == 0:
            total += dict[c]
        elif last > dict[c]:
            total -= dict[c]
        else:
            total += dict[c]
        last = dict[c]
    return total

# my method
"""
def solution(roman):
    s_v = {"I": 1,
           "V": 5,
           "X": 10,
           "L": 50,
           "C": 100,
           "D": 500,
           "M": 1000}
    roman = roman[::-1]
    sum = s_v[roman[0]]
    for i in range(1, len(roman)):
        if s_v[roman[i]] >= s_v[roman[i - 1]]:
            sum += s_v[roman[i]]
        else:
            sum -= s_v[roman[i]]
    return sum
"""