# cjavdev's solution

## to Say in the Ruby Track

Published at Mar 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".

For installation and learning resources, refer to the Ruby resources page.

For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:

``````gem install minitest
``````

If you would like color output, you can `require 'minitest/pride'` in the test file, or note the alternative instruction, below, for running the test file.

Run the tests from the exercise directory using the following command:

``````ruby say_test.rb
``````

To include color from the command line:

``````ruby -r minitest/pride say_test.rb
``````

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

### say_test.rb

``````require 'minitest/autorun'
require_relative 'say'

# Common test data version: 1.2.0 a0cee46
class SayTest < Minitest::Test
def test_zero
# skip
number = 0
assert_equal "zero", Say.new(number).in_english
end

def test_one
skip
number = 1
assert_equal "one", Say.new(number).in_english
end

def test_fourteen
skip
number = 14
assert_equal "fourteen", Say.new(number).in_english
end

def test_twenty
skip
number = 20
assert_equal "twenty", Say.new(number).in_english
end

def test_twenty_two
skip
number = 22
assert_equal "twenty-two", Say.new(number).in_english
end

def test_one_hundred
skip
number = 100
assert_equal "one hundred", Say.new(number).in_english
end

def test_one_hundred_twenty_three
skip
number = 123
assert_equal "one hundred twenty-three", Say.new(number).in_english
end

def test_one_thousand
skip
number = 1_000
assert_equal "one thousand", Say.new(number).in_english
end

def test_one_thousand_two_hundred_thirty_four
skip
number = 1_234
assert_equal "one thousand two hundred thirty-four", Say.new(number).in_english
end

def test_one_million
skip
number = 1_000_000
assert_equal "one million", Say.new(number).in_english
end

def test_one_million_two_thousand_three_hundred_forty_five
skip
number = 1_002_345
assert_equal "one million two thousand three hundred forty-five", Say.new(number).in_english
end

def test_one_billion
skip
number = 1_000_000_000
assert_equal "one billion", Say.new(number).in_english
end

def test_a_big_number
skip
number = 987_654_321_123
assert_equal "nine hundred eighty-seven billion six hundred fifty-four million three hundred twenty-one thousand one hundred twenty-three", Say.new(number).in_english
end

def test_numbers_below_zero_are_out_of_range
skip
number = -1
assert_raises(ArgumentError) do
Say.new(number).in_english
end
end

def test_numbers_above_999_999_999_999_are_out_of_range
skip
number = 1_000_000_000_000
assert_raises(ArgumentError) do
Say.new(number).in_english
end
end
end``````
``````class Say
ENGLISH = {
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",
}

ENGLISH_TENS = {
1 => "ten",
2 => "twenty",
3 => "thirty",
4 => "forty",
5 => "fifty",
6 => "sixty",
7 => "seventy",
8 => "eighty",
9 => "ninty",
}

def initialize(number)
@number = number
end

def in_english
raise ArgumentError.new if number < 0 || number >= 1_000_000_000_000
result = []
current = number

[
[1_000_000_000, "billion"],
[1_000_000, "million"],
[1_000, "thousand"],
[100, "hundred"],
].each do |mod, name|
if current / mod > 0
value = current / mod
current %= mod
result << "#{ Say.new(value).in_english } #{ name }"
end
end

if current < 20
result << ENGLISH[current]
elsif current < 100
tens = ENGLISH_TENS[current / 10]
result << tens
if current % 10 != 0
result[-1] += "-#{ ENGLISH[current % 10] }"
end
end

result.pop if result.length > 1 && result.last == "zero"
result.join(" ")
end

private
end``````

### 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?