Avatar of kwaghmode

kwaghmode's solution

to Roman Numerals in the Kotlin Track

Published at Oct 06 2019 · 0 comments
Test suite


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

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


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


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.


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.


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

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

    companion object {
        @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")

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

object RomanNumeral {
    private val romanMapping = mapOf(1000 to 'M', 100 to 'C', 10 to 'X', 5 to 'V', 1 to 'I')

    fun value(int: Int): String {
        var temp = int
        var str = ""
        while (temp != 0) {
            val key = romanMapping.keys.find { temp >= it }
            str += romanMapping[key]
            temp -= key!!
        return str
                .replace("IIIII", "V")
                .replace("IIII", "IV")
                .replace("VV", "X")
                .replace("VIV", "IX")
                .replace("XXXXX", "L")
                .replace("XXXX", "XL")
                .replace("LL", "C")
                .replace("LXL", "XC")
                .replace("CCCCC", "D")
                .replace("CCCC", "CD")
                .replace("DD", "M")
                .replace("DCD", "CM")

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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?