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:
If you're on a Mac, shell out to Mac OS X's say
program to talk out
loud.
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:
For installation and learning resources, refer to the exercism help 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
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.
require 'minitest/autorun'
require_relative 'say'
# Common test data version: 1.0.0 be403e1
class SayTest < Minitest::Test
def test_zero
# skip
question = 0
assert_equal('zero', Say.new(question).in_english)
end
def test_one
skip
question = 1
assert_equal('one', Say.new(question).in_english)
end
def test_fourteen
skip
question = 14
assert_equal('fourteen', Say.new(question).in_english)
end
def test_twenty
skip
question = 20
assert_equal('twenty', Say.new(question).in_english)
end
def test_twenty_two
skip
question = 22
assert_equal('twenty-two', Say.new(question).in_english)
end
def test_one_hundred
skip
question = 100
assert_equal('one hundred', Say.new(question).in_english)
end
def test_one_hundred_twenty_three
skip
question = 123
assert_equal('one hundred twenty-three', Say.new(question).in_english)
end
def test_one_thousand
skip
question = 1_000
assert_equal('one thousand', Say.new(question).in_english)
end
def test_one_thousand_two_hundred_thirty_four
skip
question = 1_234
assert_equal('one thousand two hundred thirty-four', Say.new(question).in_english)
end
def test_one_million
skip
question = 1_000_000
assert_equal('one million', Say.new(question).in_english)
end
def test_one_million_two_thousand_three_hundred_forty_five
skip
question = 1_002_345
assert_equal('one million two thousand three hundred forty-five', Say.new(question).in_english)
end
def test_one_billion
skip
question = 1_000_000_000
assert_equal('one billion', Say.new(question).in_english)
end
def test_a_big_number
skip
question = 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(question).in_english)
end
def test_numbers_below_zero_are_out_of_range
skip
question = -1
assert_raises ArgumentError do
Say.new(question).in_english
end
end
def test_numbers_above_999999999999_are_out_of_range
skip
question = 1_000_000_000_000
assert_raises ArgumentError do
Say.new(question).in_english
end
end
# Problems in exercism evolve over time, as we find better ways to ask
# questions.
# The version number refers to the version of the problem you solved,
# not your solution.
#
# Define a constant named VERSION inside of the top level BookKeeping
# module, which may be placed near the end of your file.
#
# In your file, it will look like this:
#
# module BookKeeping
# VERSION = 1 # Where the version number matches the one in the test.
# end
#
# If you are curious, read more about constants on RubyDoc:
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html
def test_bookkeeping
skip
assert_equal 1, BookKeeping::VERSION
end
end
class Say
DICTIONARY = {
ones: %w(
zero one two three four five six seven eight nine
ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen
),
tens: %w(zero ten twenty thirty forty fifty sixty seventy eighty ninety),
hundred: 'hundred',
powers_of_thousand: %w(one thousand million billion)
}
MAX_POWER_OF_THOUSAND = DICTIONARY[:powers_of_thousand].length - 1
LOWER_BOUND = 0
UPPER_BOUND = 1000 ** (MAX_POWER_OF_THOUSAND + 1)
def initialize(number)
check_bounds(number)
@number = number.to_i
end
def in_english
return DICTIONARY[:ones][0] if @number == 0
chunks_in_english(@number).join(' ')
end
def os_x_say_in_english
number_in_english = in_english
%x(say #{ number_in_english })
number_in_english
end
private
def check_bounds(number)
raise ArgumentError, "The number must be less than #{ UPPER_BOUND }" if number >= UPPER_BOUND
raise ArgumentError, "The number cannot be less than #{ LOWER_BOUND }" if number < LOWER_BOUND
end
def chunks_in_english(number)
chunks = chunks(number).map.with_index do |chunk, index|
power_of_thousand = MAX_POWER_OF_THOUSAND - index
chunk_in_english(chunk, power_of_thousand)
end
chunks.compact
end
def chunks(number)
chunks = (MAX_POWER_OF_THOUSAND + 1).times.with_object([]) do |_, chunks|
number, chunk = number.divmod(1000)
chunks << chunk
end
chunks.reverse
end
def chunk_in_english(chunk, power_of_thousand)
if chunk == 0
nil
else
chunk_in_english = under_1000_in_english(chunk)
scale = power_of_thousand > 0 ? "#{ DICTIONARY[:powers_of_thousand][power_of_thousand] }" : nil
[chunk_in_english, scale].compact.join(' ')
end
end
def under_1000_in_english(number)
if number < 100
under_100_in_english(number)
else
hundreds, tens = number.divmod(100)
hundreds_in_english = under_20_in_english(hundreds)
tens_in_english = tens == 0 ? nil : under_100_in_english(tens)
[hundreds_in_english, DICTIONARY[:hundred], tens_in_english].compact.join(' ')
end
end
def under_100_in_english(number)
if number < 20
under_20_in_english(number)
else
tens, ones = number.divmod(10)
tens_in_english = DICTIONARY[:tens][tens]
ones_in_english = ones == 0 ? nil : under_20_in_english(ones)
[tens_in_english, ones_in_english].compact.join('-')
end
end
def under_20_in_english(number)
DICTIONARY[:ones][number]
end
end
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.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments