Last active
April 22, 2019 09:25
-
-
Save mauricioaniche/4b97b82bc3737e2397b773e09942be7c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tudelft.sqt; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class RomanNumeral { | |
private static Map<Character, Integer> map; | |
static { | |
map = new HashMap<Character, Integer>() {{ | |
put('I', 1); | |
put('V', 5); | |
put('X', 10); | |
put('L', 50); | |
put('C', 100); | |
put('D', 500); | |
put('M', 1000); | |
}}; | |
} | |
public int romanToInt(String s) { | |
int convertedNumber = 0; | |
for(int i = 0; i < s.length(); i++) { | |
if(!map.containsKey(s.charAt(i))) | |
throw new RuntimeException("Invalid number"); | |
int currentNumber = map.get(s.charAt(i)); | |
int next = i+1 < s.length() ? | |
map.get(s.charAt(i+1)) : 0; | |
if(currentNumber > next) | |
convertedNumber += currentNumber; | |
else | |
convertedNumber -= currentNumber; | |
} | |
return convertedNumber; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package tudelft.sqt; | |
import org.junit.jupiter.api.Assertions; | |
import org.junit.jupiter.api.Test; | |
import static org.junit.jupiter.api.Assertions.assertThrows; | |
public class RomanNumeralTest { | |
@Test | |
void singleDigit() { | |
Assertions.assertEquals(1, new RomanNumeral().romanToInt("I")); | |
Assertions.assertEquals(5, new RomanNumeral().romanToInt("V")); | |
Assertions.assertEquals(10, new RomanNumeral().romanToInt("X")); | |
Assertions.assertEquals(50, new RomanNumeral().romanToInt("L")); | |
Assertions.assertEquals(100, new RomanNumeral().romanToInt("C")); | |
Assertions.assertEquals(500, new RomanNumeral().romanToInt("D")); | |
Assertions.assertEquals(1000, new RomanNumeral().romanToInt("M")); | |
} | |
@Test | |
void repetition() { | |
Assertions.assertEquals(2, new RomanNumeral().romanToInt("II")); | |
Assertions.assertEquals(20, new RomanNumeral().romanToInt("XX")); | |
} | |
@Test | |
void manyLettersInOrder() { | |
Assertions.assertEquals(6, new RomanNumeral().romanToInt("VI")); | |
Assertions.assertEquals(15, new RomanNumeral().romanToInt("XV")); | |
} | |
@Test | |
void subtractiveNotation() { | |
Assertions.assertEquals(4, new RomanNumeral().romanToInt("IV")); | |
Assertions.assertEquals(9, new RomanNumeral().romanToInt("IX")); | |
} | |
@Test | |
void withAndWithoutSubtractiveNotation() { | |
Assertions.assertEquals(14, new RomanNumeral().romanToInt("XIV")); | |
Assertions.assertEquals(159, new RomanNumeral().romanToInt("CLIX")); | |
} | |
@Test | |
void minAndMax() { | |
Assertions.assertEquals(1, new RomanNumeral().romanToInt("I")); | |
Assertions.assertEquals(3999, new RomanNumeral().romanToInt("MMMCMXCIX")); | |
} | |
@Test | |
void invalidLetter() { | |
Exception e = assertThrows(RuntimeException.class, () -> new RomanNumeral().romanToInt("Y")); | |
Assertions.assertEquals("Invalid number", e.getMessage()); | |
} | |
// TODO: We still miss testing for invalid numbers, such as 'IIIVII'. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment