Skip to content

Instantly share code, notes, and snippets.

@joseraya
Created April 14, 2012 15:02
Show Gist options
  • Select an option

  • Save joseraya/2385047 to your computer and use it in GitHub Desktop.

Select an option

Save joseraya/2385047 to your computer and use it in GitHub Desktop.
Roman Numerals Kata
package kata
import org.scalatest.matchers.ShouldMatchers
import org.scalatest.{BeforeAndAfterEach, FlatSpec}
class Roman extends FlatSpec with ShouldMatchers {
case class Symbol(latin:Int, roman:String)
var symbols = List(Symbol(5,"V"), Symbol(4, "IV"), Symbol(1,"I"))
def toRoman(number:Int): String = {
var result = ""
var remaining = number
symbols.foreach(symbol => {
1 to (remaining / symbol.latin) foreach { _ =>
result = result + symbol.roman
remaining = remaining - symbol.latin
}
})
result
}
it should "convert a 1 to I" in {
assert(toRoman(1) === "I")
}
it should "convert a 2 to II" in {
assert(toRoman(2) === "II")
}
it should "convert a 5 to V" in {
assert(toRoman(5) === "V")
}
it should "convert a 4 to IV" in {
assert(toRoman(5) === "V")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment