tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
|
|
@ -0,0 +1,13 @@
|
|||
val romanDigits = Map(
|
||||
1 -> "I", 5 -> "V",
|
||||
10 -> "X", 50 -> "L",
|
||||
100 -> "C", 500 -> "D",
|
||||
1000 -> "M",
|
||||
4 -> "IV", 9 -> "IX",
|
||||
40 -> "XL", 90 -> "XC",
|
||||
400 -> "CD", 900 -> "CM")
|
||||
val romanDigitsKeys = romanDigits.keysIterator.toList sortBy (x => -x)
|
||||
def toRoman(n: Int): String = romanDigitsKeys find (_ >= n) match {
|
||||
case Some(key) => romanDigits(key) + toRoman(n - key)
|
||||
case None => ""
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
def toRoman( v:Int ) : String = {
|
||||
val romanNumerals = 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")
|
||||
|
||||
var n = v
|
||||
romanNumerals.foldLeft(""){(s,t) => {val c = n/t._1; n = n-t._1*c; s + (t._2 * c) } }
|
||||
}
|
||||
|
||||
// A small test
|
||||
def test( arabic:Int ) = println( arabic + " => " + toRoman( arabic ) )
|
||||
|
||||
test(1990)
|
||||
test(2008)
|
||||
test(1666)
|
||||
Loading…
Add table
Add a link
Reference in a new issue