# artemkorsakov's solution

## to Roman Numerals in the Java Track

Published at Feb 08 2019 · 0 comments
Instructions
Test suite
Solution

Write a function to convert from normal numbers to Roman Numerals.

The Romans were a clever bunch. They conquered most of Europe and ruled it for hundreds of years. They invented concrete and straight roads and even bikinis. One thing they never discovered though was the number zero. This made writing and dating extensive histories of their exploits slightly more challenging, but the system of numbers they came up with is still in use today. For example the BBC uses Roman numerals to date their programmes.

The Romans wrote numbers using letters - I, V, X, L, C, D, M. (notice these letters have lots of straight lines and are hence easy to hack into stone tablets).

`````` 1  => I
10  => X
7  => VII
``````

There is no need to be able to convert numbers larger than about 3000. (The Romans themselves didn't tend to go any higher)

Wikipedia says: Modern Roman numerals ... are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero.

To see this in practice, consider the example of 1990.

In Roman numerals 1990 is MCMXC:

1000=M 900=CM 90=XC

2008 is written as MMVIII:

2000=MM 8=VIII

# Running the tests

You can run all the tests for an exercise by entering

``````\$ gradle test
``````

## Source

The Roman Numeral Kata http://codingdojo.org/cgi-bin/index.pl?KataRomanNumerals

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### RomanNumeralsTest.java

``````import org.junit.Test;
import org.junit.Ignore;

import static org.junit.Assert.assertEquals;

public class RomanNumeralsTest {

private RomanNumeral romanNumeral;

@Test
public void test1ToRomanNumberI() {
romanNumeral = new RomanNumeral(1);
assertEquals("I", romanNumeral.getRomanNumeral());
}

@Ignore("Remove to run test")
@Test
public void test2ToRomanNumberII() {
romanNumeral = new RomanNumeral(2);
assertEquals("II", romanNumeral.getRomanNumeral());
}

@Ignore("Remove to run test")
@Test
public void test3ToRomanNumberIII() {
romanNumeral = new RomanNumeral(3);
assertEquals("III", romanNumeral.getRomanNumeral());
}

@Ignore("Remove to run test")
@Test
public void test4ToRomanNumberIV() {
romanNumeral = new RomanNumeral(4);
assertEquals("IV", romanNumeral.getRomanNumeral());
}

@Ignore("Remove to run test")
@Test
public void test5ToRomanNumberV() {
romanNumeral = new RomanNumeral(5);
assertEquals("V", romanNumeral.getRomanNumeral());
}

@Ignore("Remove to run test")
@Test
public void test6ToRomanNumberVI() {
romanNumeral = new RomanNumeral(6);
assertEquals("VI", romanNumeral.getRomanNumeral());
}

@Ignore("Remove to run test")
@Test
public void test9ToRomanNumberIX() {
romanNumeral = new RomanNumeral(9);
assertEquals("IX", romanNumeral.getRomanNumeral());
}

@Ignore("Remove to run test")
@Test
public void test27ToRomanNumberXXVII() {
romanNumeral = new RomanNumeral(27);
assertEquals("XXVII", romanNumeral.getRomanNumeral());
}

@Ignore("Remove to run test")
@Test
public void test48ToRomanNumberXLVIII() {
romanNumeral = new RomanNumeral(48);
assertEquals("XLVIII", romanNumeral.getRomanNumeral());
}

@Ignore("Remove to run test")
@Test
public void test49ToRomanNumberXLIX() {
romanNumeral = new RomanNumeral(49);
assertEquals("XLIX", romanNumeral.getRomanNumeral());
}

@Ignore("Remove to run test")
@Test
public void test59ToRomanNumberLIX() {
romanNumeral = new RomanNumeral(59);
assertEquals("LIX", romanNumeral.getRomanNumeral());
}

@Ignore("Remove to run test")
@Test
public void test93ToRomanNumberXCIII() {
romanNumeral = new RomanNumeral(93);
assertEquals("XCIII", romanNumeral.getRomanNumeral());
}

@Ignore("Remove to run test")
@Test
public void test141ToRomanNumberCXLI() {
romanNumeral = new RomanNumeral(141);
assertEquals("CXLI", romanNumeral.getRomanNumeral());
}

@Ignore("Remove to run test")
@Test
public void test163ToRomanNumberCLXIII() {
romanNumeral = new RomanNumeral(163);
assertEquals("CLXIII", romanNumeral.getRomanNumeral());
}

@Ignore("Remove to run test")
@Test
public void test402ToRomanNumberCDII() {
romanNumeral = new RomanNumeral(402);
assertEquals("CDII", romanNumeral.getRomanNumeral());
}

@Ignore("Remove to run test")
@Test
public void test575ToRomanNumberDLXXV() {
romanNumeral = new RomanNumeral(575);
assertEquals("DLXXV", romanNumeral.getRomanNumeral());
}

@Ignore("Remove to run test")
@Test
public void test911ToRomanNumberCMXI() {
romanNumeral = new RomanNumeral(911);
assertEquals("CMXI", romanNumeral.getRomanNumeral());
}

@Ignore("Remove to run test")
@Test
public void test1024ToRomanNumberMXXIV() {
romanNumeral = new RomanNumeral(1024);
assertEquals("MXXIV", romanNumeral.getRomanNumeral());
}

@Ignore("Remove to run test")
@Test
public void test3000ToRomanNumberMMM() {
romanNumeral = new RomanNumeral(3000);
assertEquals("MMM", romanNumeral.getRomanNumeral());
}

}``````

### src/main/java/RomanNumeral.java

``````import java.util.Arrays;
import java.util.Collections;

class RomanNumeral {
private int number;

RomanNumeral(int number) {
this.number = number;
}

String getRomanNumeral() {
return Arrays.stream(RomanLetter.values()).sorted((o1, o2) -> o2.getArabicLetter() - (o1.getArabicLetter()))
.map(this::getRomanLetter).reduce((s1, s2) -> s1 + s2).orElse("");
}

private String getRomanLetter(RomanLetter romanLetter) {
String result = "";
int count = number / romanLetter.getArabicLetter();
if (count > 0) {
result += String.join("", Collections.nCopies(count, romanLetter.toString()));
number = number - count * romanLetter.getArabicLetter();
}
int diff = number - (romanLetter.getArabicLetter() - romanLetter.getPreviousLetter());
if (diff >= 0) {
RomanLetter prev = RomanLetter.valueOf(romanLetter.getPreviousLetter());
result += prev.name() + romanLetter.name();
number = diff;
}
return result;
}
}``````

### src/main/java/RomanLetter.java

``````import java.util.Arrays;

enum RomanLetter {
I(1),
V(5),
X(10),
L(50),
C(100),
D(500),
M(1000);

private final int arabic;

RomanLetter(int arabic) {
this.arabic = arabic;
}

static RomanLetter valueOf(int i) {
return Arrays.stream(RomanLetter.values()).filter(rl -> rl.arabic == i).findFirst().orElse(null);
}

int getArabicLetter() {
return arabic;
}

int getPreviousLetter() {
return arabic >= 500 ? 100 : arabic >= 50 ? 10 : arabic >= 5 ? 1 : 0;
}
}``````