改进我的罗马数字解码器的更好方法

A better way on improving my roman numeral decoder

快速解释,我最近开始使用 codewars 来进一步提高我的编程技能,我的第一个挑战是制作一个罗马数字解码器,我经历了很多版本,因为我对我所拥有的不满意,所以我问如果有一种更简单的方法来处理罗马数字所具有的所有模式,例如 I 是 1 但如果 I 在另一个数字旁边它会把它拿走例如 V = 5 但 IV = 4.

这是我的代码:

function Roman_Numerals_Decoder (roman)
    local Dict = {I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000}
    local number = 0
    local i = 1
    while i < #roman + 1 do
        local letter = roman:sub(i,i) -- Gets the current character in the string roman
        if roman:sub(i,i) == "I" and roman:sub(i + 1,i + 1) ~= "I" and roman:sub(i + 1,i + 1) ~= "" then -- Checks for the I pattern when I exists and next isnt I
                number = number + (Dict[roman:sub(i +1,i + 1)] - Dict[roman:sub(i,i)]) -- Taking one away from the next number
                i = i + 2 -- Increase the counter
        else
            number = number + Dict[letter] -- Adds the numbers together if no pattern is found, currently checking only I
            i = i + 1
        end
    end
    return number
end

print(Roman_Numerals_Decoder("MXLIX")) -- 1049 = MXLIX , 2008 = MMVIII

目前我正在尝试让 1049 (MXLIX) 正常工作,但我得到的是 1069,显然我没有遵守规则,我觉得它错得更离谱,因为通常如果它不正确1 或 2 个数字错误。

算法略有不同:当前一个字符的权重小于下一个字符时,您需要考虑减法。

 function Roman_Numerals_Decoder (roman)
    local Dict = {I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000}
    local num = 0
    local i = 1
    for i=1, #roman-1 do
        local letter = roman:sub(i,i) -- Gets the current character in the string roman
        local letter_p = roman:sub(i+1,i+1) 
        if (Dict[letter] < Dict[letter_p]) then
            num = num - Dict[letter] -- Taking one away from the next number
            print("-",Dict[letter],num)
        else
            num = num + Dict[letter] -- Adds the numbers together if no pattern is found, currently checking only I    
            print("+",Dict[letter],num)
        end
    end
    num = num + Dict[roman:sub(-1)];
    print("+",Dict[roman:sub(-1)], num)
    return num
end

print(Roman_Numerals_Decoder("MXLIX")) -- 1049 = MXLIX , 2008 = MMVIII