 # eitansuez's solution

## to Roman Numerals in the Kotlin Track

Published at Oct 07 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

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

## Setup

Go through the setup instructions for Kotlin to install the necessary dependencies:

https://exercism.io/tracks/kotlin/installation

## Making the test suite pass

Execute the tests with:

``````\$ gradlew test
``````

Use `gradlew.bat` if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the `@Ignore` annotation.

## 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.

### RomanNumeralTest.kt

``````import org.junit.Test
import org.junit.Ignore
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import kotlin.test.assertEquals

@RunWith(Parameterized::class)
class RomanNumeralsTest(val input: Int, val expectedOutput: String) {

companion object {
@JvmStatic
@Parameterized.Parameters(name = "{index}: romanNumeral({0})={1}")
fun data() = listOf(
arrayOf(1, "I"),
arrayOf(2, "II"),
arrayOf(3, "III"),
arrayOf(4, "IV"),
arrayOf(5, "V"),
arrayOf(6, "VI"),
arrayOf(9, "IX"),
arrayOf(27, "XXVII"),
arrayOf(48, "XLVIII"),
arrayOf(49, "XLIX"),
arrayOf(59, "LIX"),
arrayOf(93, "XCIII"),
arrayOf(141, "CXLI"),
arrayOf(163, "CLXIII"),
arrayOf(402, "CDII"),
arrayOf(575, "DLXXV"),
arrayOf(911, "CMXI"),
arrayOf(1024, "MXXIV"),
arrayOf(3000, "MMM")
)
}

@Test
fun test() {
assertEquals(expectedOutput, RomanNumeral.value(input))
}

}``````
``````import java.lang.StringBuilder

object RomanNumeral {

fun value(input: Int): String {
val b = StringBuilder()
var rest = input
for (i in 1..rest.div(1000)) {
b.append("M")
}
rest = rest.rem(1000)
if (rest.div(100) == 9) {
b.append("CM")
} else {
for (i in 1..rest.div(500)) {
b.append("D")
}
rest = rest.rem(500)
val hundreds = rest.div(100)
if (hundreds > 3) {
b.append("CD")
} else {
for (i in 1..hundreds) {
b.append("C")
}
}
}
rest = rest.rem(100)
if (rest.div(10) == 9) {
b.append("XC")
} else {
for (i in 1..rest.div(50)) {
b.append("L")
}
rest = rest.rem(50)
val tens = rest.div(10)
if (tens > 3) {
b.append("XL")
} else {
for (i in 1..tens) {
b.append("X")
}
}
}
rest = rest.rem(10)
if (rest.div(1) == 9) {
b.append("IX")
} else {
for (i in 1..rest.div(5)) {
b.append("V")
}
rest = rest.rem(5)
val ones = rest.div(1)
if (ones > 3) {
b.append("IV")
} else {
for (i in 1..ones) {
b.append("I")
}
}
}
return b.toString()
}

}``````