Published at Oct 14 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

See also: http://www.novaroma.org/via_romana/numbers.html

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

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

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.

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

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

```
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.IllegalArgumentException
object RomanNumeral {
private val romanTable: List<List<String>> = "IVXLCDM??".windowed(3, 2) {
it.split("").filter(String::isNotEmpty)
}
fun value(number: Int): String {
require(number < 4000) {"so support for numbers >= 4000"}
return number.toString().reversed().map { it.toString().toInt() }.mapIndexed { index, digit ->
when (digit) {
in 1 .. 3 -> romanTable[index][0].repeat(digit)
4 -> romanTable[index][0] + romanTable[index][1]
in 5 .. 8 -> romanTable[index][1] + romanTable[index][0].repeat(digit - 5)
9 -> romanTable[index][0] + romanTable[index][2]
else -> ""
}
}.reversed().joinToString("")
}
}
```

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,092 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments