Skip to content

Instantly share code, notes, and snippets.

@mettledrum
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mettledrum/b85768cc5a1e85f8b9c5 to your computer and use it in GitHub Desktop.
Save mettledrum/b85768cc5a1e85f8b9c5 to your computer and use it in GitHub Desktop.
Just a simple class to convert integers LT | 1 quadrillion | into their American English equivalent.
# Andrew Hoyle
# converts a +/- integer to its American English equivalent
# includes basic exceptions
# PRE: less than | 1 quintillion | and +/- integer
# allows only numbers, commas, and '+' or '-' in idx[0]
# POST: American English translation of number
class Worder
# AM. ENGLISH
# class variable hashes ////////////////////////////////////////////////////
@@eng_singles = {'0'=>'', '1'=>'one', '2'=>'two', '3'=>'three',
'4'=>'four', '5'=>'five', '6'=>'six', '7'=>'seven', '8'=>'eight',
'9'=>'nine'}
# the weird ones
@@eng_teens = {'10'=>'ten', '11'=>'eleven', '12'=>'twelve',
'13'=>'thirteen', '14'=>'fourteen', '15'=>'fifteen', '16'=>'sixteen',
'17'=>'seventeen', '18'=>'eighteen', '19'=>'nineteen'}
@@eng_tens = {'0'=>'', '2'=>'twenty', '3'=>'thirty', '4'=>'forty',
'5'=>'fifty', '6'=>'sixty', '7'=>'seventy', '8'=>'eighty',
'9'=>'ninety'}
# //////////////////////////////////////////////////////////////////////////
# calls the functions to process the number, stuffs into object variables
# includes error checking
def initialize(num)
# holds number as string; value not changed
@num = num.to_s
# will hold word version of number
@num_word = ""
# for recursive calls, value is changed
@local_num = num.to_s
# nil
if @num==""
raise "Cannot be nil. Must be a +/- integer with or without commas."
end
# check if it's negative or positive, change @local_num
if @num[0]=='-'
@num_word.insert -1, 'negative'
@local_num = @local_num[1..-1]
end
if @num[0]=='+'
@local_num = @local_num[1..-1]
end
# get rid of commas
@local_num.delete! ','
# error checking to make sure there's no weird stuff
# do after getting '-' and ','s out of there
if @local_num =~ /\D/
raise "Must be a +/- integer with or without commas."
end
# zero special case: don't process
if @num.to_i==0
@num_word = 'zero'
return
end
# then call the functions to populate the @num_word
comma_processing
end
# getter
def get_word
return @num_word
end
private
# for the stuff b/w commas: singles, tens, hundreds
def bw_commas(n)
# don't do anything if there are 0, 00, or 000s
return unless n.to_i!=0
# look at length, recursively call after lopping off characters
case n.length
# hundreds
when 3
@num_word.insert -1, @@eng_singles[n[0]]
@num_word.insert -1, 'hundred' unless n[0]=='0'
bw_commas n[1..-1]
# tens
when 2
# teens: special English #s
if @@eng_teens[n.to_s]
@num_word.insert -1, @@eng_teens[n]
# done now
return
# tens: add tens prefix, lop off one char, call again
else
@num_word.insert -1, @@eng_tens[n[0]]
bw_commas n[1..-1]
end
# singles
when 1
@num_word.insert -1, @@eng_singles[n[0]]
bw_commas n[1..-1]
# nil: done
when 0
return
else
raise "The value passed to 'bw_commas' is too lengthy!"
end
end
# figures out how much to lop off from num string
def get_idx(n)
return ((n%3)-1)%3
end
# every time there's a comma
def get_magnitude(len)
case len
when 16..18
'quadrillion'
when 13..15
'trillion'
when 10..12
'billion'
when 7..9
'million'
when 4..6
'thousand'
when 1..3
''
when 0
''
else
raise 'The number is too big!'
end
end
# for each comma, call functions recursively
def comma_processing
len = @local_num.length
local_segment = @local_num[0..get_idx(len)]
bw_commas local_segment
@num_word.insert -1, get_magnitude(len) unless local_segment.to_i==0
# reduce @local_num size for recursive call
@local_num = @local_num[get_idx(len)+1..-1]
# finish when ""
comma_processing unless len==0
end
end
# test the Worder class
while true
puts "Enter a +/- integer:"
w=Worder.new(gets[0..-2])
puts w.get_word
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment