Roman numeral converter
Roman numerals map cleanly onto a lookup table plus greedy
subtraction: pair every symbol with its numeric value, walk the table from
biggest to smallest, and for each pair keep subtracting the value (and
appending the symbol) for as long as the remaining number is big enough.
The only wrinkle is the subtractive cases - 4 is "IV", not "IIII", and
9 is "IX", not "VIIII". The clean fix is to put those combined symbols
right in the table alongside the plain ones (90 -> "XC", 40 -> "XL",
9 -> "IX", 4 -> "IV"), in order from largest to smallest. The same greedy
loop handles them automatically - no special-casing needed.
Your task: write toRoman(n) for n from 1 to 100, converting n
to its roman numeral string.
You'll practice:
- Building and walking a lookup table in priority order
- Greedy subtraction to consume a number down to zero