Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

2.4 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69f8c998d78ad3171a0713ba Challenge 287: Roman Numeral Fixer 29 challenge-287

--description--

Given a string of malformed Roman numerals, return the value in standard Roman numeral notation.

The input will only use additive notation, so each symbol adds its value to the total. As a reminder, here are the symbols and values:

Symbol Value
"I" 1
"V" 5
"X" 10
"L" 50
"C" 100
"D" 500
"M" 1000

When re-encoding, use the largest possible symbol at each step, using subtractive pairs ("IV", "IX", "XL", "XC", "CD", "CM") where needed.

--hints--

fix_numerals("XIIIII") should return "XV".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(fix_numerals("XIIIII"), "XV")`)
}})

fix_numerals("IIIILX") should return "LXIV".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(fix_numerals("IIIILX"), "LXIV")`)
}})

fix_numerals("XXVVVIIIII") should return "XL".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(fix_numerals("XXVVVIIIII"), "XL")`)
}})

fix_numerals("MDCCLXXXXVIIII") should return "MDCCXCIX".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(fix_numerals("MDCCLXXXXVIIII"), "MDCCXCIX")`)
}})

fix_numerals("IIIIVVVVXXXXLLLLCCDD") should return "MCDLXIV".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(fix_numerals("IIIIVVVVXXXXLLLLCCDD"), "MCDLXIV")`)
}})

fix_numerals("ILCDMIVDIIXLCVCXDL") should return "MMCMLXXXIV".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(fix_numerals("ILCDMIVDIIXLCVCXDL"), "MMCMLXXXIV")`)
}})

--seed--

--seed-contents--

def fix_numerals(s):

    return s

--solutions--

def fix_numerals(s):
    values = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
    encoding = [
        (1000, "M"), (900, "CM"), (500, "D"), (400, "CD"),
        (100, "C"), (90, "XC"), (50, "L"), (40, "XL"),
        (10, "X"), (9, "IX"), (5, "V"), (4, "IV"), (1, "I")
    ]

    total = sum(values[c] for c in s)

    result = ""
    for value, symbol in encoding:
        while total >= value:
            result += symbol
            total -= value
    return result