Skip to content

Instantly share code, notes, and snippets.

@tamizhgeek
Created November 17, 2014 14:51
Show Gist options
  • Save tamizhgeek/67b2bbf014e7dc73f7c4 to your computer and use it in GitHub Desktop.
Save tamizhgeek/67b2bbf014e7dc73f7c4 to your computer and use it in GitHub Desktop.
Number to words problem solution. Tests here : https://gist.github.com/tamizhgeek/06f923330b130d701d09
package assignments
class NumberToWords(number : Int) {
val digitToWord = Map(
0 -> "zero",
1 -> "one",
2 -> "two",
3 -> "three",
4 -> "four",
5 -> "five",
6 -> "six",
7 -> "seven",
8 -> "eight",
9 -> "nine"
)
def numForWords() = {
wordFor(number, List()).mkString("-")
}
def wordFor(in: Int, res : List[String]) : List[String] = {
if(in < 10)
digitToWord.apply(in) :: res
else
wordFor(in / 10, digitToWord.apply(in % 10) :: res)
}
}
object NumberToWords extends App {
val number = new NumberToWords(103)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment