-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRomanNumerals.java
More file actions
163 lines (134 loc) · 5.43 KB
/
RomanNumerals.java
File metadata and controls
163 lines (134 loc) · 5.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package dev;
import java.io.*;
import java.util.*;
public class RomanNumerals {
// Roman numeral values in descending order for conversion to minimal form
private static final int[] VALUES = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
private static final String[] NUMERALS = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
/**
* Converts a Roman numeral string to its integer value
*/
public static int romanToInt(String roman) {
Map<Character, Integer> values = new HashMap<>();
values.put('I', 1);
values.put('V', 5);
values.put('X', 10);
values.put('L', 50);
values.put('C', 100);
values.put('D', 500);
values.put('M', 1000);
int result = 0;
int prevValue = 0;
// Process from right to left
for (int i = roman.length() - 1; i >= 0; i--) {
int currentValue = values.get(roman.charAt(i));
// If current value is less than previous, subtract it (subtractive notation)
if (currentValue < prevValue) {
result -= currentValue;
} else {
result += currentValue;
}
prevValue = currentValue;
}
return result;
}
/**
* Converts an integer to its minimal Roman numeral representation
*/
public static String intToMinimalRoman(int num) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < VALUES.length; i++) {
while (num >= VALUES[i]) {
result.append(NUMERALS[i]);
num -= VALUES[i];
}
}
return result.toString();
}
/**
* Calculates the total character savings by converting Roman numerals to minimal form
*/
public static int calculateSavings(String filename) throws IOException {
int totalSavings = 0;
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (!line.isEmpty()) {
// Convert Roman numeral to integer
int value = romanToInt(line);
// Convert integer back to minimal Roman numeral
String minimal = intToMinimalRoman(value);
// Calculate savings
int savings = line.length() - minimal.length();
totalSavings += savings;
// Debug output for first few examples
if (totalSavings <= 50) { // Show first few for verification
System.out.println(line + " (" + value + ") -> " + minimal +
" (saved: " + savings + " characters)");
}
}
}
}
return totalSavings;
}
/**
* Process Roman numerals directly from a string containing all the data
*/
public static int calculateSavingsFromString(String data) {
int totalSavings = 0;
String[] lines = data.split("\n");
for (String line : lines) {
line = line.trim();
if (!line.isEmpty()) {
// Convert Roman numeral to integer
int value = romanToInt(line);
// Convert integer back to minimal Roman numeral
String minimal = intToMinimalRoman(value);
// Calculate savings
int savings = line.length() - minimal.length();
totalSavings += savings;
// Debug output for first few examples
if (totalSavings <= 50) { // Show first few for verification
System.out.println(line + " (" + value + ") -> " + minimal +
" (saved: " + savings + " characters)");
}
}
}
return totalSavings;
}
public static void main(String[] args) {
try {
System.out.println("Roman Numeral Optimizer");
System.out.println("=======================");
System.out.println();
// Sample data from your document (first few lines for testing)
String sampleData = """
MMMMDCLXXII
MMDCCCLXXXIII
MMMDLXVIIII
MMMMDXCV
DCCCLXXII
MMCCCVI
MMMCDLXXXVII
MMMMCCXXI
MMMCCXX
MMMMDCCCLXXIII
""";
System.out.println("Processing sample data:");
int sampleSavings = calculateSavingsFromString(sampleData);
System.out.println("Sample savings: " + sampleSavings);
System.out.println();
// For file processing
String filename = "roman.txt";
int totalSavings = calculateSavings(filename);
System.out.println("Total characters saved from file: " + totalSavings);
} catch (IOException e) {
System.err.println("Could not read file (this is expected if file doesn't exist)");
System.out.println("You can copy the Roman numerals to a file named 'roman.txt' to process the full dataset.");
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
}