 paulfioravanti's solution

to Say in the Ruby Track

Published at Jul 13 2018 · 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
# frozen_string_literal: true

class Say
MAX_RANGE = (0..999_999_999_999).freeze
private_constant :MAX_RANGE

module Number
NUMBERS = %w[
zero one two three four five six seven eight nine ten eleven twelve
thirteen fourteen fifteen sixteen seventeen eighteen nineteen
].freeze
private_constant :NUMBERS
TENS = %w[
nil nil twenty thirty forty fifty sixty seventy eighty ninety
].freeze
private_constant :TENS
NUMBER_COMPOUNDS = lambda do |number|
case number.to_s.size
when 2
[10, TENS[number / 10]]
when 3
[100, "hundred"]
when 4..6
[1000, "thousand"]
when 7..9
[1_000_000, "million"]
when 10..12
[1_000_000_000, "billion"]
end
end
private_constant :NUMBER_COMPOUNDS

module_function

def in_english(number)
if number < 20
NUMBERS[number]
elsif number < 100
build_hyphened_word(number)
else
build_full_word(number)
end
end

def build_hyphened_word(number)
build_word(number) do |word, _divisor|
[word, ->(num) { "-#{NUMBERS[num]}" }]
end
end
private_class_method :build_hyphened_word

def build_full_word(number)
build_word(number) do |word, divisor|
[
"#{in_english(number / divisor)} #{word}",
->(num) { " #{in_english(num)}" }
]
end
end
private_class_method :build_full_word

def build_word(number)
divisor, word = NUMBER_COMPOUNDS.call(number)
head, remainder_function = yield(word, divisor)
tail =
if (num = number % divisor).nonzero?
remainder_function.call(num)
else
""
end
end
private_class_method :build_word
end
private_constant :Number

def initialize(number)
@number = number
end

def in_english
raise ArgumentError unless MAX_RANGE.cover?(number)

Number.in_english(number)
end

private

end