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

快速解释一下,我最近开始使用 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 个数字是错误的.

回答1

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

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

相似文章

最新文章