Skip to content

Commit 5246f63

Browse files
authored
Enhance docs, remove main. add more tests (#5925)
1 parent 0feb416 commit 5246f63

File tree

2 files changed

+73
-48
lines changed

2 files changed

+73
-48
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,68 @@
11
package com.thealgorithms.conversions;
22

33
/**
4-
* Converting Integers into Roman Numerals
4+
* A utility class to convert integers into Roman numerals.
55
*
6-
* <p>
7-
* ('I', 1); ('IV',4); ('V', 5); ('IX',9); ('X', 10); ('XL',40); ('L', 50);
8-
* ('XC',90); ('C', 100); ('D', 500); ('M', 1000);
6+
* <p>Roman numerals follow these rules:
7+
* <ul>
8+
* <li>I = 1</li>
9+
* <li>IV = 4</li>
10+
* <li>V = 5</li>
11+
* <li>IX = 9</li>
12+
* <li>X = 10</li>
13+
* <li>XL = 40</li>
14+
* <li>L = 50</li>
15+
* <li>XC = 90</li>
16+
* <li>C = 100</li>
17+
* <li>D = 500</li>
18+
* <li>M = 1000</li>
19+
* </ul>
20+
*
21+
* <p>Conversion is based on repeatedly subtracting the largest possible Roman numeral value
22+
* from the input number until it reaches zero. For example, 1994 is converted as:
23+
* <pre>
24+
* 1994 -> MCMXCIV (1000 + 900 + 90 + 4)
25+
* </pre>
926
*/
1027
public final class IntegerToRoman {
28+
29+
// Array of Roman numeral values in descending order
30+
private static final int[] ALL_ROMAN_NUMBERS_IN_ARABIC = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
31+
32+
// Corresponding Roman numeral symbols
33+
private static final String[] ALL_ROMAN_NUMBERS = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
34+
1135
private IntegerToRoman() {
1236
}
1337

14-
private static final int[] ALL_ROMAN_NUMBERS_IN_ARABIC = new int[] {
15-
1000,
16-
900,
17-
500,
18-
400,
19-
100,
20-
90,
21-
50,
22-
40,
23-
10,
24-
9,
25-
5,
26-
4,
27-
1,
28-
};
29-
private static final String[] ALL_ROMAN_NUMBERS = new String[] {
30-
"M",
31-
"CM",
32-
"D",
33-
"CD",
34-
"C",
35-
"XC",
36-
"L",
37-
"XL",
38-
"X",
39-
"IX",
40-
"V",
41-
"IV",
42-
"I",
43-
};
44-
45-
// Value must be > 0
38+
/**
39+
* Converts an integer to its Roman numeral representation.
40+
* Steps:
41+
* <ol>
42+
* <li>Iterate over the Roman numeral values in descending order</li>
43+
* <li>Calculate how many times a numeral fits</li>
44+
* <li>Append the corresponding symbol</li>
45+
* <li>Subtract the value from the number</li>
46+
* <li>Repeat until the number is zero</li>
47+
* <li>Return the Roman numeral representation</li>
48+
* </ol>
49+
*
50+
* @param num the integer value to convert (must be greater than 0)
51+
* @return the Roman numeral representation of the input integer
52+
* or an empty string if the input is non-positive
53+
*/
4654
public static String integerToRoman(int num) {
4755
if (num <= 0) {
4856
return "";
4957
}
5058

5159
StringBuilder builder = new StringBuilder();
52-
53-
for (int a = 0; a < ALL_ROMAN_NUMBERS_IN_ARABIC.length; a++) {
54-
int times = num / ALL_ROMAN_NUMBERS_IN_ARABIC[a];
55-
for (int b = 0; b < times; b++) {
56-
builder.append(ALL_ROMAN_NUMBERS[a]);
57-
}
58-
59-
num -= times * ALL_ROMAN_NUMBERS_IN_ARABIC[a];
60+
for (int i = 0; i < ALL_ROMAN_NUMBERS_IN_ARABIC.length; i++) {
61+
int times = num / ALL_ROMAN_NUMBERS_IN_ARABIC[i];
62+
builder.append(ALL_ROMAN_NUMBERS[i].repeat(Math.max(0, times)));
63+
num -= times * ALL_ROMAN_NUMBERS_IN_ARABIC[i];
6064
}
6165

6266
return builder.toString();
6367
}
64-
65-
public static void main(String[] args) {
66-
System.out.println(IntegerToRoman.integerToRoman(2131));
67-
}
6868
}

src/test/java/com/thealgorithms/conversions/IntegerToRomanTest.java

+25
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,30 @@ public class IntegerToRomanTest {
1010
public void testIntegerToRoman() {
1111
assertEquals("MCMXCIV", IntegerToRoman.integerToRoman(1994));
1212
assertEquals("LVIII", IntegerToRoman.integerToRoman(58));
13+
assertEquals("IV", IntegerToRoman.integerToRoman(4));
14+
assertEquals("IX", IntegerToRoman.integerToRoman(9));
15+
assertEquals("MMM", IntegerToRoman.integerToRoman(3000));
16+
}
17+
18+
@Test
19+
public void testSmallNumbers() {
20+
assertEquals("I", IntegerToRoman.integerToRoman(1));
21+
assertEquals("II", IntegerToRoman.integerToRoman(2));
22+
assertEquals("III", IntegerToRoman.integerToRoman(3));
23+
}
24+
25+
@Test
26+
public void testRoundNumbers() {
27+
assertEquals("X", IntegerToRoman.integerToRoman(10));
28+
assertEquals("L", IntegerToRoman.integerToRoman(50));
29+
assertEquals("C", IntegerToRoman.integerToRoman(100));
30+
assertEquals("D", IntegerToRoman.integerToRoman(500));
31+
assertEquals("M", IntegerToRoman.integerToRoman(1000));
32+
}
33+
34+
@Test
35+
public void testEdgeCases() {
36+
assertEquals("", IntegerToRoman.integerToRoman(0)); // Non-positive number case
37+
assertEquals("", IntegerToRoman.integerToRoman(-5)); // Negative number case
1338
}
1439
}

0 commit comments

Comments
 (0)