From ca9bd0e7efb749dc2a367911b21f42ccd98a94e5 Mon Sep 17 00:00:00 2001 From: Ashwanth Kumar Date: Mon, 9 Jun 2014 22:02:49 +0530 Subject: [PATCH] refactoring number to words --- src/main/scala/tron/NumberToWords.scala | 18 ++++++++++++------ src/test/scala/tron/NumberToWordsTest.scala | 13 +++++++++++-- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main/scala/tron/NumberToWords.scala b/src/main/scala/tron/NumberToWords.scala index a5bfb9c..00b20d6 100644 --- a/src/main/scala/tron/NumberToWords.scala +++ b/src/main/scala/tron/NumberToWords.scala @@ -1,5 +1,6 @@ package tron + class NumberToWords { val singleDigitsMap = toLongMap(Map (0-> "zero", 1 -> "one", 2 -> "two", 3 -> "three", 4 -> "four", 5 -> "five", 6 -> "six", 7 -> "seven", @@ -19,7 +20,7 @@ class NumberToWords { if (number == 0) { "" } - else if (twoDigitMap.exists { case(k, v) => k == number }) { + else if (twoDigitMap.contains(number)) { twoDigitMap(number) } else { @@ -27,9 +28,10 @@ class NumberToWords { val tensPlace = (number/10)%10 val unitsPlace = number % 10 if (hundredsPlace == 0) - twoDigitMap(tensPlace * 10) + " " + twoDigitMap(unitsPlace) - else - singleDigitsMap(hundredsPlace) + " hundred " + solveForThreeDigits(number % 100) + combine(twoDigitMap(tensPlace * 10) , singleDigitsMap(unitsPlace)) + else { + combine(singleDigitsMap(hundredsPlace), "hundred" , solveForThreeDigits(number % 100)) + } } } @@ -38,9 +40,9 @@ class NumberToWords { case 0 => "" case n if n/1000 == 0 => solveForThreeDigits(n) case _ => - val (power, w) = xs.head + val (power, illion) = xs.head if (number / power > 0) { - solveForThreeDigits(number / power) + " " + w + " " + solveInternal(number % power, xs.tail) + combine(solveForThreeDigits(number / power), illion , solveInternal(number % power, xs.tail)) } else { solveInternal(number , xs.tail) } @@ -55,5 +57,9 @@ class NumberToWords { } } + def combine(parts: String*) = { + parts.foldLeft(""){case (soFar, part) => if(part.isEmpty) soFar.trim else soFar + " " + part.trim} + } + def toLongMap(m: Map[Int,String]) = m.map { case (a,b) => a.toLong -> b} } \ No newline at end of file diff --git a/src/test/scala/tron/NumberToWordsTest.scala b/src/test/scala/tron/NumberToWordsTest.scala index 1a7e6f9..ec3ef9b 100644 --- a/src/test/scala/tron/NumberToWordsTest.scala +++ b/src/test/scala/tron/NumberToWordsTest.scala @@ -7,7 +7,7 @@ class NumberToWordsTest extends FlatSpec with ShouldMatchers { "Number To Words Test" should "return the correct words for single digit numbers" in { val numbersMap = Map (0-> "zero", 1 -> "one", 2 -> "two", 3 -> "three", 4 -> "four", 5 -> "five", 6 -> "six", 7 -> "seven", - 8 -> "eight", 9-> "nine") + 8 -> "eight", 9-> "nine") numbersMap.foreach { case (number, word) => new NumberToWords().solve(number) should equal(word) @@ -60,4 +60,13 @@ class NumberToWordsTest extends FlatSpec with ShouldMatchers { new NumberToWords().solve(3436454643L) should equal("three billion four hundred thirty six million four hundred fifty four thousand six hundred forty three") new NumberToWords().solve(432003436454643L) should equal("four hundred thirty two trillion three billion four hundred thirty six million four hundred fifty four thousand six hundred forty three") } -} + + "Number To Words Test" should "return the correct words for corner cases" in { + new NumberToWords().solve(100000) should equal("one hundred thousand") + new NumberToWords().solve(100000000) should equal("one hundred million") + new NumberToWords().solve(375000100) should equal("three hundred seventy five million one hundred") + } + + + +} \ No newline at end of file