Back to questions
Before you solve this question, make sure to solve the easier warmup problem Roman to Integer, where you need to write a function that converts Roman numerals to integers.
In this problem, we're going the opposite direction – turning an integer into roman numerals. To refresh your memory, these seven different symbols represent Roman numerals with the following values:
Symbol | Value |
---|---|
I | 1 |
V | 5 |
X | 10 |
L | 50 |
C | 100 |
D | 500 |
M | 1000 |
Roman numerals are formed by converting decimal place values from highest to lowest. When converting an integer to a Roman numeral, follow these rules:
For numbers that don’t start with 4 or 9: Pick the symbol with the largest value that can be subtracted from the number. Append that symbol to the result, subtract its value, and repeat the process with the remaining number.
For numbers that start with 4 or 9: Use a subtractive form. This means you write one smaller symbol before a larger one. For example:
Symbols can be repeated up to three times for powers of 10: Only the symbols for 1 (I), 10 (X), 100 (C), and 1000 (M) can be repeated consecutively, up to three times, to represent multiples of those values. Symbols like 5 (V), 50 (L), and 500 (D) cannot be repeated. If you need to append a symbol four times, use the subtractive form instead (like "IV" for 4).
Given an integer less than 4000, return the Roman numeral representation of that number.
Input: $num = 30$
Output: "XXX"
Explanation:
Input: $num = 69$
Output: "LXIX"
Explanation:
Let’s focus on the information in the question.
The first one is pretty straightforward. If it doesn't start with 4 or 9, we will just subtract the maximum value that we can subtract from the number and append the symbol. Instead of subtracting, you can also use modulo; the result of the subtraction of the maximum number and the modulo of the number with the maximum number is the same. For example, for 56, the maximum number we can subtract is 50, and the result after subtraction is 6. Also, 56 mod 50 is 6, so you can use either.
The second one is no different from the first one; it is just that we have other numbers than the usual ones (I, V, X, L, C, D, M). You will do the same for the cases when two numbers are interpreted together. For example, for 46, the maximum number that we can subtract is 40, but 40 is not in the usual ones (single characters), so we will be including the combined ones (IV, IX, XL, XC, CD, CM) into consideration.
In the third case, you may be worried that you will append a single character more than 3 times, but if you follow the other steps correctly, here is how we will handle how many times we will add a single character:
Here is the code to do that:
What we have been doing so far was following the steps listed in the question, but if we try to simplify it, what if we map the value of each digit? For example, for the thousands digit, we map values for 1000, 2000, and 3000 (since the number is always less than 4000). For the hundreds digit, we will map values for 100, 200, 300, ..., 900. For the tens digit, we will map 10, 20, 30, ..., 90, and for the ones place, we will store values of 1, 2, 3, ..., 9. Here are the Roman numerals for each digit:
Place | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---|---|---|---|---|---|---|---|---|---|
Thousands | M | MM | MMM | ||||||
Hundreds | C | CC | CCC | CD | D | DC | DCC | DCCC | CM |
Tens | X | XX | XXX | XL | L | LX | LXX | LXXX | XC |
Ones | I | II | III | IV | V | VI | VII | VIII | IX |
Finally, we will extract each digit, starting from the thousands to the ones. We can achieve this using integer division and modulo operations:
To make it simpler, let's define four different arrays for each digit and then place the value of each in its corresponding index. For example, the Roman numeral value of 700 will be placed at the 7th index of the hundreds digit, and 800 will be at the 8th. 50 will be at the 5th index of the tens digit. Each of the digits will have an empty string at the 0th index since it is possible that a number may have a 0 at some digits.
Here is the code: