Skip to content
DeepForgeby svx
All problems
MediumAlgorithmsal-070

Integer to Roman

Problem

Convert an integer num between 1 and 3999 to its Roman numeral string.

Greedily subtract the largest table value that fits, where the table also contains the subtractive pairs such as CM (900), CD (400) and IV (4). The output is the standard shortest Roman form.

Hint

Include 4, 9, 40, 90, 400 and 900 directly in the value table so no special cases are needed.

Test cases

case 1
in: [4]
expected: "IV"
case 2
in: [9]
expected: "IX"
case 3
in: [58]
expected: "LVIII"
case 4
in: [1994]
expected: "MCMXCIV"
case 5
in: [3999]
expected: "MMMCMXCIX"
Starter codePython
def int_to_roman(num):
    # Your code here
    pass

5 test cases · run your code against them in the editor