Published at Jul 13 2018
·
1 comment

Instructions

Test suite

Solution

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

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

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`

.

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.

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".

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.

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".

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

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

```
(ns say-test
(:require [clojure.test :refer [deftest is]]
say))
(deftest zero-test
(is (= "zero" (say/number 0))))
(deftest one-test
(is (= "one" (say/number 1))))
(deftest fourteen-test
(is (= "fourteen" (say/number 14))))
(deftest twenty-test
(is (= "twenty" (say/number 20))))
(deftest twenty-two-test
(is (= "twenty-two" (say/number 22))))
(deftest one-hundred-test
(is (= "one hundred" (say/number 100))))
(deftest one-hundred-twenty-three-test
(is (= "one hundred twenty-three" (say/number 123))))
(deftest one-thousand-test
(is (= "one thousand" (say/number 1000))))
(deftest one-thousand-two-hundred-thirty-four-test
(is (= "one thousand two hundred thirty-four" (say/number 1234))))
(deftest one-million-test
(is (= "one million" (say/number 1000000))))
(deftest one-million-two-thousand-three-hundred-forty-five-test
(is (= "one million two thousand three hundred forty-five" (say/number 1002345))))
(deftest one-billion-test
(is (= "one billion" (say/number 1000000000))))
(deftest a-big-number-test
(is (= "nine hundred eighty-seven billion six hundred fifty-four million three hundred twenty-one thousand one hundred twenty-three" (say/number 987654321123))))
(deftest below-zero-is-out-of-range-test
(is (thrown? IllegalArgumentException (say/number -1))))
(deftest numbers-above-999999999999-out-of-range-test
(is (thrown? IllegalArgumentException (say/number 1000000000000))))
```

```
(ns say)
(def wordbook
{0 "zero"
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"
; double are used to store names of large numbers
; one can read it six zeros after the digit is "million"
2.0 "hundred"
3.0 "thousand"
6.0 "million"
9.0 "billion"
12.0 "trillion"
15.0 "quadrillion"
18.0 "quintillion"
21.0 "sextillion"
24.0 "septillion"
27.0 "octillion"
30.0 "nonillion"
33.0 "decillion"
36.0 "undecillion"
39.0 "duodecillion"
42.0 "tredecillion"
45.0 "quattuordecillion"
48.0 "quindecillion"
51.0 "sexdecillion"
54.0 "septendecillion"
57.0 "octodecillion"
60.0 "novemdecillion"
63.0 "vigintillion"
303.0 "centillion"})
(defn- pad [s]
(if s (str " " s) s))
(defn- rem1 [num div]
(let [a (rem num div)]
(if (zero? a) div a)))
(defn- eng [xs]
(let [[a b c] xs]
(case (count xs)
0 nil
1 (wordbook a)
2 (let [a (* a 10)]
(wordbook
(+ a b)
(str (wordbook a) "-" (wordbook b))))
3 (str
(wordbook a)
(pad (wordbook 2.0))
(pad (eng (drop-while zero? [b c]))))
(let [[h t] (split-at (rem1 (count xs) 3) xs)]
(str (eng h)
(pad (wordbook (double (count t))))
(pad (eng (drop-while zero? t))))))))
(defn number [n]
(or (< -1 n 1e12)
(throw (IllegalArgumentException. "out of range argument")))
(eng (map #(Character/getNumericValue %) (str n))))
```

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?

## Community comments

reworked to allow up to 306 digits. pretty happy with solution now but was wondering if rem1 could be simplified or renamed