Last active
December 10, 2015 19:29
-
-
Save rainzoo/4482104 to your computer and use it in GitHub Desktop.
Convert numbers to words, numbers up to thousands.
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
object NumberToWords { | |
val units = List("one", "two", "three", "four", | |
"five", "six", "seven", "eight", "nine") | |
val teens = List("ten", "eleven", "twelve", "thirteen", | |
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen") | |
val tens = List("twenty", "thirty", "forty", | |
"fifty", "sixty", "seventy", "eighty", "ninety") | |
def main(args: Array[String]) { | |
//Solves Project Euler problem 17, http://projecteuler.net/problem=17 | |
println(Range(1, 1001).map((i: Int) => convert6(i). | |
filterNot((p: Char) => p == ' ' | p == '-').length) | |
.foldLeft(0)((m: Int, n: Int) => m + n)) | |
//To test: Range( 1, 1000).map((i:Int) => println(convert6(i))) | |
} | |
def digit(n: Int, b: Int) = { | |
(n / b, n % b) | |
} | |
def convert2(n: Int): String = { | |
val v = digit(n, 10) | |
combine2(v._1, v._2) | |
} | |
def convert3(n: Int) = { | |
val v = digit(n, 100) | |
combine3(v._1, v._2) | |
} | |
def convert6(n: Int): String = { | |
val v = digit(n, 1000) | |
combine6(v._1, v._2) | |
} | |
def combine2(u: Int, v: Int): String = (u, v) match { | |
case (0, v) => units(v - 1) | |
case (1, v) => teens(v) | |
case (u, 0) => tens(u - 2) | |
case (u, v) => tens(u - 2) + "-" + units(v - 1) | |
} | |
def combine3(u: Int, v: Int) = (u, v) match { | |
case (0, v) => convert2(v) | |
case (u, 0) => units(u - 1) + " hundred" | |
case (u, v) => units(u - 1) + " hundred and " + convert2(v) | |
} | |
def link(n: Int): String = n < 100 match { | |
case true => " and " | |
case false => " " | |
} | |
def combine6(u: Int, v: Int): String = (u, v) match { | |
case (0, v) => convert3(v) | |
case (u, 0) => convert3(u) + " thousand" | |
case (u, v) => convert3(u) + " thousand" + link(v - 1) + convert3(v) | |
} | |
}object NumberToWords { | |
val units = List("one", "two", "three", "four", | |
"five", "six", "seven", "eight", "nine") | |
val teens = List("ten", "eleven", "twelve", "thirteen", | |
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen") | |
val tens = List("twenty", "thirty", "forty", | |
"fifty", "sixty", "seventy", "eighty", "ninety") | |
def main(args: Array[String]) { | |
val test = Range( 1, 10000) | |
test.foreach((i:Int) => println(convert6(i))) | |
} | |
def digit(n: Int, b: Int) = { | |
(n / b, n % b) | |
} | |
def convert2(n: Int): String = { | |
val v = digit(n, 10) | |
combine2(v._1, v._2) | |
} | |
def convert3(n: Int) = { | |
val v = digit(n, 100) | |
combine3(v._1, v._2) | |
} | |
def convert6(n: Int): String = { | |
val v = digit(n, 1000) | |
combine6(v._1, v._2) | |
} | |
def combine2(u: Int, v: Int): String = (u, v) match { | |
case (0, v) => units(v - 1) | |
case (1, v) => teens(v) | |
case (u, 0) => tens(u - 2) | |
case (u, v) => tens(u - 2) + "-" + units(v - 1) | |
} | |
def combine3(u: Int, v: Int) = (u, v) match { | |
case (0, v) => convert2(v) | |
case (u, 0) => units(u - 1) + " hundred" | |
case (u, v) => units(u - 1) + " hundred and " + convert2(v) | |
} | |
def link(n: Int): String = n < 100 match { | |
case true => " and " | |
case false => " " | |
} | |
def combine6(u: Int, v: Int): String = (u, v) match { | |
case (0, v) => convert3(v) | |
case (u, 0) => convert3(u) + " thousand" | |
case (u, v) => convert3(u) + " thousand" + link(v - 1) + convert3(v) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment