← All Exercises

Roman Numeral Conversion

basicjavascript~10min

Background

Roman numerals still appear in outlines, copyright dates, and clock faces. The greedy subtractive table is the shortest correct conversion algorithm.

Problem

Convert each integer to its Roman numeral string using the value table 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 in descending order. Output one numeral per input number.

Input

A JSON object {"numbers": number[]} with integers from 1 to 3999.

Output

A JSON object {"roman": string[]} in input order.

Sample tests

Sample 1

Input: {"numbers":[1994,2026,3999,58]}
Expected: {"roman":["MCMXCIV","MMXXVI","MMMCMXCIX","LVIII"]}

Sample 2

Input: {"numbers":[1,4,9,40,90,400,900]}
Expected: {"roman":["I","IV","IX","XL","XC","CD","CM"]}

Your solution

Learning objective

Apply greedy decomposition over a denomination table with subtractive pairs.