Created
November 17, 2014 14:51
-
-
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
This file contains 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
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