🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of SergiiVlasiuk

SergiiVlasiuk's solution

to Roman Numerals in the Scala Track

Published at Aug 25 2019 · 0 comments
Test suite

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


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 unfolding is a logical addition to and part of the FP standard repertoire.

This exercise can be seen as a case for unfolding: "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.


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.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") {
    RomanNumerals.roman(2) should be ("II")

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  test("3000 is three M's") {
    RomanNumerals.roman(3000) should be ("MMM")
class RomanNumerals(num: Int) {
  lazy val roman = RomanNumerals.thousands(index(1000)) +
    RomanNumerals.hundreds(index(100)) +
    RomanNumerals.tens(index(10)) +

  def index(digit: Int): Int = num % (digit * 10) / digit

object RomanNumerals {
  def roman: Int => String = num => new RomanNumerals(num).roman

  val ones = List("", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX")
  val tens = List("", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC")
  val hundreds = List("", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM")
  val thousands = List("", "M", "MM", "MMM")

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?