Skip to content

[SCALA-649] Convert a Number to Roman Numeral in Scala #1455

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.baeldung.scala.romannumerals

import scala.annotation.tailrec
import scala.math.BigDecimal.int2bigDecimal

object NumberToRomanNumeral {

private val numbersToRomans = List(
(1000, "M"),
(900, "CM"),
(500, "D"),
(400, "CD"),
(100, "C"),
(90, "XC"),
(50, "L"),
(40, "XL"),
(10, "X"),
(9, "IX"),
(5, "V"),
(4, "IV"),
(1, "I")
)

def usingRecursion(num: Int): String = {
numbersToRomans.find((n, _) => {
num - n >= 0
}) match
case Some((n, r)) => r + usingRecursion(num - n)
case None => ""
}

def usingTailRecursion(num: Int): String = {
@tailrec
def recursiveFn(remaining: Int, acc: String): String = {
numbersToRomans.find((n, _) => {
remaining - n >= 0
}) match
case Some((n, r)) => recursiveFn(remaining - n, acc + r)
case None => acc
}
recursiveFn(num, "")
}

def usingFold(num: Int): String = {
numbersToRomans
.foldLeft((num, ""))((remainingAndAcc, numAndRoman) => {
val (remaining, acc) = remainingAndAcc
if (remaining == 0) remainingAndAcc
else {
val (n, r) = numAndRoman

(remaining % n, acc + r * (remaining / n))
}
})
._2
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.baeldung.scala.romannumeral

import com.baeldung.scala.romannumerals.NumberToRomanNumeral
import org.scalatest.matchers.should.Matchers
import org.scalatest.prop.TableDrivenPropertyChecks
import org.scalatest.wordspec.AnyWordSpec

class NumberToRomanNumeralUnitTest
extends AnyWordSpec
with Matchers
with TableDrivenPropertyChecks {
val testValuesWithResults = Table(
("Number", "Roman"),
(0, ""),
(1, "I"),
(2, "II"),
(4, "IV"),
(5, "V"),
(6, "VI"),
(9, "IX"),
(10, "X"),
(11, "XI"),
(14, "XIV"),
(15, "XV"),
(17, "XVII"),
(19, "XIX"),
(20, "XX"),
(40, "XL"),
(44, "XLIV"),
(49, "XLIX"),
(50, "L"),
(90, "XC"),
(100, "C"),
(400, "CD"),
(500, "D"),
(900, "CM"),
(1000, "M"),
(1949, "MCMXLIX")
)

"Correction roman numeral should be returned for number" should {
"using usingRecursion" in {
forAll(testValuesWithResults)((num, roman) => {
NumberToRomanNumeral.usingRecursion(num) shouldBe roman
})
}

"using usingTailRecursion" in {
forAll(testValuesWithResults)((num, roman) => {
NumberToRomanNumeral.usingTailRecursion(num) shouldBe roman
})
}

"using usingFold" in {
forAll(testValuesWithResults)((num, roman) => {
NumberToRomanNumeral.usingFold(num) shouldBe roman
})
}
}

}