Created
September 5, 2020 09:24
-
-
Save yogonza524/b71347c5e0ae0c59f5c99bac51a9a50f 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
import static org.junit.jupiter.api.Assertions.assertEquals; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.function.BiFunction; | |
import org.junit.jupiter.api.BeforeEach; | |
import org.junit.jupiter.api.Test; | |
import org.junit.platform.commons.util.StringUtils; | |
public class RomanNumberTest { | |
private Map<Character, Integer> numbers; | |
private Map<Character, Integer> facu; | |
@BeforeEach | |
public void init() { | |
numbers = new HashMap<>(); | |
facu = new HashMap<>(); | |
numbers.put('I', 1); | |
numbers.put('V', 5); | |
numbers.put('X', 10); | |
numbers.put('L', 50); | |
numbers.put('C', 100); | |
numbers.put('D', 500); | |
numbers.put('M', 1000); | |
facu.put('F', 1); | |
facu.put('A', 5); | |
facu.put('C', 10); | |
facu.put('U', 50); | |
facu.put('N', 100); | |
facu.put('D', 500); | |
facu.put('O', 1000); | |
} | |
public enum LastNumber { | |
YES, | |
NO | |
} | |
@Test | |
public void shouldConvertAllNumbers() { | |
assertEquals(1914, transform.apply("MCMXIV", numbers)); | |
assertEquals(14, transform.apply("XIV", numbers)); | |
assertEquals(10, transform.apply("X", numbers)); | |
assertEquals(4, transform.apply("IV", numbers)); | |
assertEquals(554, transform.apply("DLIV", numbers)); | |
assertEquals(303, transform.apply("CCCIII", numbers)); | |
assertEquals(909, transform.apply("CMIX", numbers)); | |
assertEquals(3000, transform.apply("MMM", numbers)); | |
} | |
@Test | |
public void shouldConvertAllNumbersFacu() { | |
assertEquals(1914, transform.apply("ONOCFA", facu)); | |
assertEquals(14, transform.apply("CFA", facu)); | |
assertEquals(10, transform.apply("C", facu)); | |
assertEquals(4, transform.apply("FA", facu)); | |
assertEquals(554, transform.apply("DUFA", facu)); | |
assertEquals(303, transform.apply("NNNFFF", facu)); | |
assertEquals(909, transform.apply("NOFC", facu)); | |
assertEquals(3000, transform.apply("OOO", facu)); | |
} | |
private BiFunction<String, Map<Character, Integer>, Integer> transform = | |
(input, map) -> | |
input.length() == 1 | |
? map.get(input.charAt(0)) | |
: sum(input.charAt(0), input.substring(1), 0, LastNumber.NO, map); | |
private BiFunction<Character, Map<Character, Integer>, Integer> convert = | |
(digit, map) -> map.get(digit); | |
private int sum( | |
char head, String tail, int acum, LastNumber lastNumber, Map<Character, Integer> map) { | |
if (StringUtils.isBlank(tail) && lastNumber.equals(LastNumber.YES)) | |
return acum + convert.apply(head, map); | |
if (StringUtils.isBlank(tail)) return acum; | |
char nextDigit = tail.charAt(0); | |
int first = convert.apply(head, map); | |
int second = convert.apply(nextDigit, map); | |
if (first >= second) { | |
LastNumber flag = first == second ? LastNumber.YES : LastNumber.NO; | |
return sum(nextDigit, tail.substring(1), acum + first, flag, map); | |
} | |
int offset = tail.length() > 1 ? 1 : 0; | |
return sum( | |
tail.charAt(offset), | |
tail.substring(offset + 1), | |
acum + (second - first), | |
offset > 0 ? LastNumber.YES : LastNumber.NO, | |
map); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment