Skip to content

Commit fbb6817

Browse files
committed
test: linter test
1 parent 333750e commit fbb6817

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Solution class for converting an integer to a Roman numeral.
3+
*/
4+
5+
class Solution {
6+
public String intToRoman(int num) {
7+
StringBuilder roman = new StringBuilder();
8+
int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
9+
String[] symbols = {
10+
"M",
11+
"CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"
12+
};
13+
for (int i = 0; i < values.length; i++) {
14+
while (num >= values[i]) {
15+
roman.append(symbols[i]);
16+
num -= values[i];
17+
}
18+
}
19+
return roman.toString();
20+
}
21+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Integer to Roman
2+
3+
Seven different symbols represent Roman numerals with the following values:
4+
5+
| Symbol | Value |
6+
|--------|-------|
7+
| I | 1 |
8+
| V | 5 |
9+
| X | 10 |
10+
| L | 50 |
11+
| C | 100 |
12+
| D | 500 |
13+
| M | 1000 |
14+
15+
Roman numerals are formed by appending the conversions of decimal place values from highest to lowest. Converting a decimal place value into a Roman numeral follows these rules:
16+
17+
- If the value does **not** start with 4 or 9, select the symbol of the maximal value that can be subtracted from the input, append that symbol to the result, subtract its value, and convert the remainder to a Roman numeral.
18+
- If the value **starts with 4 or 9**, use the subtractive form representing one symbol subtracted from the following symbol. For example:
19+
- 4 is 1 (I) less than 5 (V): **IV**
20+
- 9 is 1 (I) less than 10 (X): **IX**
21+
- Only the following subtractive forms are used: 4 (**IV**), 9 (**IX**), 40 (**XL**), 90 (**XC**), 400 (**CD**), and 900 (**CM**).
22+
- Only powers of 10 (**I**, **X**, **C**, **M**) can be appended consecutively at most 3 times to represent multiples of 10.
23+
- You cannot append 5 (**V**), 50 (**L**), or 500 (**D**) multiple times. If you need to append a symbol 4 times, use the subtractive form.
24+
25+
Given an integer, convert it to a Roman numeral.
26+
27+
---
28+
29+
## Examples
30+
31+
### Example 1
32+
33+
**Input:** `num = 3749`
34+
**Output:** `"MMMDCCXLIX"`
35+
36+
**Explanation:**
37+
- 3000 = MMM as 1000 (M) + 1000 (M) + 1000 (M)
38+
- 700 = DCC as 500 (D) + 100 (C) + 100 (C)
39+
- 40 = XL as 10 (X) less than 50 (L)
40+
- 9 = IX as 1 (I) less than 10 (X)
41+
42+
> Note: 49 is not 1 (I) less than 50 (L) because the conversion is based on decimal places.
43+
44+
---
45+
46+
### Example 2
47+
48+
**Input:** `num = 58`
49+
**Output:** `"LVIII"`
50+
51+
**Explanation:**
52+
- 50 = L
53+
- 8 = VIII
54+
55+
---
56+
57+
### Example 3
58+
59+
**Input:** `num = 1994`
60+
**Output:** `"MCMXCIV"`
61+
62+
**Explanation:**
63+
- 1000 = M
64+
- 900 = CM
65+
- 90 = XC
66+
- 4 = IV
67+
68+
---
69+
70+
## Constraints
71+
72+
```
73+
1 <= num <= 3999
74+
```
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
Module for converting integers to Roman numerals.
3+
4+
This module contains a Solution class with a method intToRoman that converts
5+
an integer to its corresponding Roman numeral representation.
6+
"""
7+
8+
class Solution(object):
9+
def intToRoman(self, num):
10+
"""
11+
:type num: int
12+
:rtype: str
13+
"""
14+
roman_str = ""
15+
potencies = [1000, 100, 10, 1]
16+
roman_equivalences = {
17+
1: 'I',
18+
5: 'V',
19+
10: 'X',
20+
50: 'L',
21+
100: 'C',
22+
500: 'D',
23+
1000: 'M',
24+
}
25+
26+
for power in potencies:
27+
# Gets the digit at the current power (e.g. 4532, power=1000 -> 4)
28+
digit = int(num / power)
29+
# Gets the value at the current power (e.g. 4*1000=4000)
30+
power_digit = digit * power
31+
# Removes the processed digit from num (e.g. 4532 % 1000 = 532)
32+
num = num % power
33+
34+
# Skip if there is no digit at this power
35+
if digit == 0:
36+
continue
37+
# Direct mapping exists (e.g. 1000, 500, 100, etc.)
38+
if power_digit in roman_equivalences:
39+
roman_str += roman_equivalences[power_digit]
40+
continue
41+
# Handle subtractive notation (e.g. 4=IV, 9=IX, 40=XL, etc.)
42+
if digit in (4, 9):
43+
prefix = roman_equivalences[power]
44+
roman_str += (prefix + roman_equivalences[power * (digit + 1)])
45+
continue
46+
# For digits 6-8, add the '5' symbol and repeat the '1' symbol as needed
47+
if digit > 5:
48+
roman_str += roman_equivalences[power * 5]
49+
digit -= 5
50+
51+
# Append the '1' symbol as many times as needed
52+
roman_str += roman_equivalences[power] * digit
53+
return roman_str

0 commit comments

Comments
 (0)