Created
July 4, 2012 16:07
-
-
Save AndyBowes/3048075 to your computer and use it in GitHub Desktop.
Scala implementation of Roman Numeral conversion
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RomanNumeral { | |
def toRomanNumerals( number: Int) : String = { | |
toRomanNumerals( number, List( ("M", 1000),("CM", 900), ("D", 500), ("CD", 400), ("C", 100), ("XC", 90), | |
("L", 50), ("XL",40), ("X", 10), ("IX", 9), ("V", 5), ("IV", 4), ("I", 1) )) | |
} | |
private def toRomanNumerals( number: Int, digits: List[(String, Int)] ) : String = digits match { | |
case Nil => "" | |
case h :: t => h._1 * ( number / h._2 ) + toRomanNumerals( number % h._2, t ) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment