Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring number to words #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/main/scala/tron/NumberToWords.scala
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -19,17 +20,18 @@ class NumberToWords {
if (number == 0) {
""
}
else if (twoDigitMap.exists { case(k, v) => k == number }) {
else if (twoDigitMap.contains(number)) {
twoDigitMap(number)
}
else {
val hundredsPlace = number / 100
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))
}
}
}

Expand All @@ -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)
}
Expand All @@ -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}
}
13 changes: 11 additions & 2 deletions src/test/scala/tron/NumberToWordsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}



}