Skip to content

Commit 76317f8

Browse files
authored
Merge branch 'master' into master
2 parents a0b0e1c + f4d9ba7 commit 76317f8

91 files changed

Lines changed: 4713 additions & 539 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.algorithms.romannumerals;
2+
3+
import java.util.List;
4+
5+
class RomanArabicConverter {
6+
7+
public static int romanToArabic(String input) {
8+
String romanNumeral = input.toUpperCase();
9+
int result = 0;
10+
11+
List<RomanNumeral> romanNumerals = RomanNumeral.getReverseSortedValues();
12+
13+
int i = 0;
14+
15+
while ((romanNumeral.length() > 0) && (i < romanNumerals.size())) {
16+
RomanNumeral symbol = romanNumerals.get(i);
17+
if (romanNumeral.startsWith(symbol.name())) {
18+
result += symbol.getValue();
19+
romanNumeral = romanNumeral.substring(symbol.name().length());
20+
} else {
21+
i++;
22+
}
23+
}
24+
if (romanNumeral.length() > 0) {
25+
throw new IllegalArgumentException(input + " cannot be converted to a Roman Numeral");
26+
}
27+
28+
return result;
29+
}
30+
31+
public static String arabicToRoman(int number) {
32+
if ((number <= 0) || (number > 4000)) {
33+
throw new IllegalArgumentException(number + " is not in range (0,4000]");
34+
}
35+
36+
List<RomanNumeral> romanNumerals = RomanNumeral.getReverseSortedValues();
37+
38+
int i = 0;
39+
StringBuilder sb = new StringBuilder();
40+
41+
while (number > 0 && i < romanNumerals.size()) {
42+
RomanNumeral currentSymbol = romanNumerals.get(i);
43+
if (currentSymbol.getValue() <= number) {
44+
sb.append(currentSymbol.name());
45+
number -= currentSymbol.getValue();
46+
} else {
47+
i++;
48+
}
49+
}
50+
return sb.toString();
51+
}
52+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.algorithms.romannumerals;
2+
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
import java.util.stream.Collectors;
7+
8+
enum RomanNumeral {
9+
I(1), IV(4), V(5), IX(9), X(10), XL(40), L(50), XC(90), C(100), CD(400), D(500), CM(900), M(1000);
10+
11+
private int value;
12+
13+
RomanNumeral(int value) {
14+
this.value = value;
15+
}
16+
17+
public int getValue() {
18+
return value;
19+
}
20+
21+
public static List<RomanNumeral> getReverseSortedValues() {
22+
return Arrays.stream(values())
23+
.sorted(Comparator.comparing((RomanNumeral e) -> e.value).reversed())
24+
.collect(Collectors.toList());
25+
}
26+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.algorithms.romannumerals;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.Test;
6+
7+
public class RomanArabicConverterUnitTest {
8+
9+
@Test
10+
public void given2018Roman_WhenConvertingToArabic_ThenReturn2018() {
11+
12+
String roman2018 = "MMXVIII";
13+
14+
int result = RomanArabicConverter.romanToArabic(roman2018);
15+
16+
assertThat(result).isEqualTo(2018);
17+
}
18+
19+
@Test
20+
public void given1999Arabic_WhenConvertingToRoman_ThenReturnMCMXCIX() {
21+
22+
int arabic1999 = 1999;
23+
24+
String result = RomanArabicConverter.arabicToRoman(arabic1999);
25+
26+
assertThat(result).isEqualTo("MCMXCIX");
27+
}
28+
29+
}

antlr/pom.xml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<artifactId>antlr</artifactId>
5+
<name>antlr</name>
6+
7+
<parent>
8+
<groupId>com.baeldung</groupId>
9+
<artifactId>parent-modules</artifactId>
10+
<version>1.0.0-SNAPSHOT</version>
11+
</parent>
12+
13+
<build>
14+
<plugins>
15+
<plugin>
16+
<groupId>org.antlr</groupId>
17+
<artifactId>antlr4-maven-plugin</artifactId>
18+
<version>${antlr.version}</version>
19+
<executions>
20+
<execution>
21+
<goals>
22+
<goal>antlr4</goal>
23+
</goals>
24+
</execution>
25+
</executions>
26+
</plugin>
27+
<plugin>
28+
<groupId>org.codehaus.mojo</groupId>
29+
<artifactId>build-helper-maven-plugin</artifactId>
30+
<version>${mojo.version}</version>
31+
<executions>
32+
<execution>
33+
<phase>generate-sources</phase>
34+
<goals>
35+
<goal>add-source</goal>
36+
</goals>
37+
<configuration>
38+
<sources>
39+
<source>${basedir}/target/generated-sources/antlr4</source>
40+
</sources>
41+
</configuration>
42+
</execution>
43+
</executions>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
<dependencies>
48+
<dependency>
49+
<groupId>org.antlr</groupId>
50+
<artifactId>antlr4-runtime</artifactId>
51+
<version>${antlr.version}</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>junit</groupId>
55+
<artifactId>junit</artifactId>
56+
<version>${junit.version}</version>
57+
<scope>test</scope>
58+
</dependency>
59+
</dependencies>
60+
<properties>
61+
<java.version>1.8</java.version>
62+
<antlr.version>4.7.1</antlr.version>
63+
<junit.version>4.12</junit.version>
64+
<mojo.version>3.0.0</mojo.version>
65+
</properties>
66+
</project>

0 commit comments

Comments
 (0)