Avatar of nickandh

nickandh's solution

to Say in the Scala Track

Published at Oct 09 2019 · 0 comments
Instructions
Test suite
Solution

Given a number from 0 to 999,999,999,999, spell out that number in English.

Step 1

Handle the basic case of 0 through 99.

If the input to the program is 22, then the output should be 'twenty-two'.

Your program should complain loudly if given a number outside the blessed range.

Some good test cases for this program are:

  • 0
  • 14
  • 50
  • 98
  • -1
  • 100

Extension

If you're on a Mac, shell out to Mac OS X's say program to talk out loud. If you're on Linux or Windows, eSpeakNG may be available with the command espeak.

Step 2

Implement breaking a number up into chunks of thousands.

So 1234567890 should yield a list like 1, 234, 567, and 890, while the far simpler 1000 should yield just 1 and 0.

The program must also report any values that are out of range.

Step 3

Now handle inserting the appropriate scale word between those chunks.

So 1234567890 should yield '1 billion 234 million 567 thousand 890'

The program must also report any values that are out of range. It's fine to stop at "trillion".

Step 4

Put it all together to get nothing but plain English.

12345 should give twelve thousand three hundred forty-five.

The program must also report any values that are out of range.

Extensions

Use and (correctly) when spelling out the number in English:

  • 14 becomes "fourteen".
  • 100 becomes "one hundred".
  • 120 becomes "one hundred and twenty".
  • 1002 becomes "one thousand and two".
  • 1323 becomes "one thousand three hundred and twenty-three".

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

Please see the learning and installation pages if you need any help.

Source

A variation on JavaRanch CattleDrive, exercise 4a http://www.javaranch.com/say.jsp

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

SayTest.scala

import org.scalatest.{Matchers, FunSuite}

/** @version 1.1.0 */
class SayTest extends FunSuite with Matchers {

  test("zero") {
    Say.inEnglish(0) should be(Some("zero"))
  }

  test("one") {
    pending
    Say.inEnglish(1) should be(Some("one"))
  }

  test("fourteen") {
    pending
    Say.inEnglish(14) should be(Some("fourteen"))
  }

  test("twenty") {
    pending
    Say.inEnglish(20) should be(Some("twenty"))
  }

  test("twenty-two") {
    pending
    Say.inEnglish(22) should be(Some("twenty-two"))
  }

  test("one hundred") {
    pending
    Say.inEnglish(100) should be(Some("one hundred"))
  }

  test("one hundred twenty-three") {
    pending
    Say.inEnglish(123) should be(Some("one hundred twenty-three"))
  }

  test("one thousand") {
    pending
    Say.inEnglish(1000) should be(Some("one thousand"))
  }

  test("one thousand two hundred thirty-four") {
    pending
    Say.inEnglish(1234) should be(Some("one thousand two hundred thirty-four"))
  }

  test("one million") {
    pending
    Say.inEnglish(1000000) should be(Some("one million"))
  }

  test("one million two thousand three hundred forty-five") {
    pending
    Say.inEnglish(1002345) should be(Some("one million two thousand three hundred forty-five"))
  }

  test("one billion") {
    pending
    Say.inEnglish(1000000000) should be(Some("one billion"))
  }

  test("a big number") {
    pending
    Say.inEnglish(987654321123l) should be(Some("nine hundred eighty-seven billion six hundred fifty-four million three hundred twenty-one thousand one hundred twenty-three"))
  }

  test("numbers below zero are out of range") {
    pending
    Say.inEnglish(-1) should be(None)
  }

  test("numbers above 999,999,999,999 are out of range") {
    pending
    Say.inEnglish(1000000000000l) should be(None)
  }
}
object Say {

  private val numberMap = Map( 1 -> "one", 2 -> "two", 3 -> "three", 4 -> "four", 5 -> "five", 6 -> "six", 7 -> "seven", 8 -> "eight", 9 -> "nine", 10 -> "ten",
    11 -> "eleven", 12 -> "twelve", 13 -> "thirteen", 14 -> "fourteen", 15 -> "fifteen", 16 -> "sixteen", 17 -> "seventeen", 18 -> "eighteen", 19 -> "nineteen",
    20 -> "twenty", 30 -> "thirty", 40 -> "forty", 50 -> "fifty", 60 -> "sixty", 70 -> "seventy", 80 -> "eighty", 90 -> "ninety" )
  private val powers = Map( 2 -> "thousand", 3 -> "million", 4 -> "billion" )


  private def checkRange(number: Long): Option[Long] = if (number < 0 || number > 999999999999L) None else Some( number )

  private implicit val strConcat: (String, String) => String = (a, b) => s"$a $b"

  private def combineAndOr[A](a: Option[A], b: Option[A])(implicit f: (A, A) => A): Option[A] = {
    (a, b) match {
      case (None, _) => b
      case (_, None) => a
      case (_, _) => combine( a, b )( f )
    }
  }

  private def combine[A](a: Option[A], b: Option[A])(implicit f: (A, A) => A): Option[A] = {
    a.flatMap( x => {
      b match {
        case Some( y ) => Some( f( x, y ) )
        case _ => Some( x )
      }
    } )
  }

  private def handleUnits(n: Integer): Option[String] = {
    if (n == 0) None
    else if (numberMap.contains( n )) numberMap.get( n )
    else combineAndOr( numberMap.get( (n / 10) * 10 ), numberMap.get( n % 10 ) )( (a, b) => s"$a-$b" )
  }

  private def handleHundreds(n: Int): Option[String] = {
    if (n == 0) None
    else combineAndOr( handleUnits( n / 100 ).map( a => s"$a hundred" ), handleUnits( n % 100 ) )
  }

  private def handleNumberBlock(scale: Int, n: Int): Option[String] = {
    combine( handleHundreds( n ), powers.get( scale ) )
  }

  /** Convert long to string and split into list with hundreds first then thousands etc  */
  private def splitStringForNumbers(l: Long): List[String] = {
    l.toString.reverse.grouped( 3 ).toList.map( _.reverse )
  }

  /**
   * @param l the long to convert
   * @return an Option wrapping the resulting String
   */
  private def buildNumberString(l: Long): Option[String] = {
    /* index the string list and translate each block to an Option[String]
       Then concatenate to a single option skipping the Nones (see combine function) **/
    splitStringForNumbers( l ).zipWithIndex.foldLeft( Nil: List[Option[String]] ) {
      case (accum, (value, index)) => handleNumberBlock( index + 1, value.toInt ) :: accum
    }.reduceLeft( combine( _, _ ) )
  }


  def inEnglish(number: Long): Option[String] = {
    checkRange( number ) match {
      case None => None
      case Some( x ) if (x == 0) => Some( "zero" )
      case _ => buildNumberString( number )
    }
  }

}

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?