 ## to Roman Numerals in the Scala Track

Published at Aug 30 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

## Hints

For something a little different you might also try a solution with an `unfold` function. You are probably already familiar with `foldLeft/Right`: "map" a whole collection into something else (usually a non-collection). `unfoldLeft/Right` are the "inverse" operations: "map" something (usually a non-collection) into a collection. So `unfold`ing is a logical addition to and part of the FP standard repertoire.

This exercise can be seen as a case for `unfold`ing: "map" an `Int` into a `String` (which is of course implicitly a `Seq[Char]`).

Unfortunately `unfoldLeft/Right` is not included in Scala's collection library. But you can take the implementation from here.

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command `sbt test` in the exercise directory.

For more detailed info about the Scala track see the help page.

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

``````import org.scalatest.{Matchers, FunSuite}

/** @version 1.2.0 */
class RomanNumeralsTest extends FunSuite with Matchers {

test("1 is a single I") {
RomanNumerals.roman(1) should be ("I")
}

test("2 is two I's") {
pending
RomanNumerals.roman(2) should be ("II")
}

test("3 is three I's") {
pending
RomanNumerals.roman(3) should be ("III")
}

test("4, being 5 - 1, is IV") {
pending
RomanNumerals.roman(4) should be ("IV")
}

test("5 is a single V") {
pending
RomanNumerals.roman(5) should be ("V")
}

test("6, being 5 + 1, is VI") {
pending
RomanNumerals.roman(6) should be ("VI")
}

test("9, being 10 - 1, is IX") {
pending
RomanNumerals.roman(9) should be ("IX")
}

test("20 is two X's") {
pending
RomanNumerals.roman(27) should be ("XXVII")
}

test("48 is not 50 - 2 but rather 40 + 8") {
pending
RomanNumerals.roman(48) should be ("XLVIII")
}

test("49 is not 40 + 5 + 4 but rather 50 - 10 + 10 - 1") {
pending
RomanNumerals.roman(49) should be ("XLIX")
}

test("50 is a single L") {
pending
RomanNumerals.roman(59) should be ("LIX")
}

test("90, being 100 - 10, is XC") {
pending
RomanNumerals.roman(93) should be ("XCIII")
}

test("100 is a single C") {
pending
RomanNumerals.roman(141) should be ("CXLI")
}

test("60, being 50 + 10, is LX") {
pending
RomanNumerals.roman(163) should be ("CLXIII")
}

test("400, being 500 - 100, is CD") {
pending
RomanNumerals.roman(402) should be ("CDII")
}

test("500 is a single D") {
pending
RomanNumerals.roman(575) should be ("DLXXV")
}

test("900, being 1000 - 100, is CM") {
pending
RomanNumerals.roman(911) should be ("CMXI")
}

test("1000 is a single M") {
pending
RomanNumerals.roman(1024) should be ("MXXIV")
}

test("3000 is three M's") {
pending
RomanNumerals.roman(3000) should be ("MMM")
}
}``````
``````object RomanNumerals {
// Use this to build output using tail recursion
def roman(input: Int): String = romanImpl(input, "")

@scala.annotation.tailrec
private def romanImpl(input: Int, output: String): String = {
input match {
case n if n >= 1000 => romanImpl(input - 1000, output + "M")
case n if n >= 900 => romanImpl(input - 900, output + "CM")
case n if n >= 500 => romanImpl(input - 500, output + "D")
case n if n >= 400 => romanImpl(input - 400, output + "CD")
case n if n >= 100 => romanImpl(input - 100, output + "C")
case n if n >= 90 => romanImpl(input - 90, output + "XC")
case n if n >= 50 => romanImpl(input - 50, output + "L")
case n if n >= 40 => romanImpl(input - 40, output + "XL")
case n if n >= 10 => romanImpl(input - 10, output + "X")
case n if n >= 9 => romanImpl(input - 9, output + "IX")
case n if n >= 5 => romanImpl(input - 5, output + "V")
case n if n >= 4 => romanImpl(input - 4, output + "IV")
case n if n >= 1 => romanImpl(input - 1, output + "I")
case _ => output
}
}
}``````