Skip to content

Commit fe1c8e2

Browse files
committed
Roman Numeral Reduction
1 parent 282e696 commit fe1c8e2

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

src/hard/RomanNumReduction.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package hard;
2+
3+
import java.util.HashMap;
4+
5+
/**
6+
* Have the function RomanNumeralReduction(str) read str
7+
* which will be a string of roman numerals in decreasing order.
8+
* The numerals being used are: I for 1, V for 5, X for 10,
9+
* L for 50, C for 100, D for 500 and M for 1000.
10+
* Your program should return the same number given
11+
* by str using a smaller set of roman numerals.
12+
* For example: if str is "LLLXXXVVVV" this is 200, so your program should return CC
13+
* because this is the shortest way to write 200 using the roman numeral system given above.
14+
* If a string is given in its shortest form, just return that same string.
15+
*/
16+
public class RomanNumReduction {
17+
18+
private static final HashMap<String, Integer> romans = new HashMap<>() {
19+
{
20+
put("M", 1000);
21+
put("D", 500);
22+
put("C", 100);
23+
put("L", 50);
24+
put("X", 10);
25+
put("V", 5);
26+
put("I", 1);
27+
}
28+
};
29+
30+
private static final String[] orderedRomans = new String[]{"M", "D", "C", "L", "X", "V", "I"};
31+
32+
/**
33+
* Calculate the sum for an expression.
34+
*
35+
* @param exp expression
36+
* @return a sum
37+
*/
38+
public static int sumRomans(String exp) {
39+
int result = 0;
40+
String[] tokens = exp.toUpperCase().split("");
41+
for (String token : tokens) {
42+
result += romans.get(token);
43+
}
44+
return result;
45+
}
46+
47+
/**
48+
* Reduce Roman numerals.
49+
*
50+
* @param sum a number
51+
* @return a string of Roman numerals
52+
*/
53+
public static String reduceRomans(int sum) {
54+
StringBuilder output = new StringBuilder();
55+
int remainder = sum;
56+
for (String numeral : orderedRomans) {
57+
Integer val = romans.get(numeral);
58+
while (remainder / val >= 0 && remainder - val >= 0) {
59+
output.append(numeral);
60+
remainder -= val;
61+
}
62+
}
63+
return output.toString();
64+
}
65+
66+
/**
67+
* Roman Numeral Reduction function.
68+
*
69+
* @param str input string
70+
* @return result
71+
*/
72+
public static String romanNumeralReduction(String str) {
73+
int sum = sumRomans(str);
74+
return reduceRomans(sum);
75+
}
76+
77+
/**
78+
* Entry point.
79+
*
80+
* @param args command line arguments
81+
*/
82+
public static void main(String[] args) {
83+
String result1 = romanNumeralReduction("XXXVVIIIIIIIIII");
84+
System.out.println(result1);
85+
String result2 = romanNumeralReduction("VVV");
86+
System.out.println(result2);
87+
String result3 = romanNumeralReduction("CCCCCIIIII");
88+
System.out.println(result3);
89+
}
90+
91+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy