Skip to content

Instantly share code, notes, and snippets.

@gustavofranke
Created August 28, 2018 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gustavofranke/f3dfd76f8eff64e8618aa793a483dea3 to your computer and use it in GitHub Desktop.
Save gustavofranke/f3dfd76f8eff64e8618aa793a483dea3 to your computer and use it in GitHub Desktop.
val toEnglish = Map((0, 4) -> "four", (1, 3) -> "thirty", (2, 2) -> "two hundreds", (3, 1) -> "one thousand")
def intToEnglish(a: Int): String = a.toString.toList
.map { _.toString.toInt }
.reverse.zipWithIndex.reverse
.map { t => toEnglish(t.swap) }
.mkString(" ")
intToEnglish(4) // res0: String = four
intToEnglish(34) // res1: String = thirty four
intToEnglish(234) // res2: String = two hundreds thirty four
intToEnglish(1234) // res3: String = one thousand two hundreds thirty four
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment