Life as Clay

Learn to Program: English Numbers method

with one comment


I’ll tell ya… I kept out-thinking myself on this one. The concept is really simple. Return the number entered as English text. All you have to do is to check if something is above a certain number then recurse through if it is. For instance, divide the number entered by 1000 to check if it is over 1000. If it is, take the number of thousands and recurse with it, tacking ‘ thousand’ onto the end. Then, subtract that number of thousands from the number originally entered and proceed through the method. This repeats several times, to accommodate numbers up to 999,999,999 in this version of the method.

This one took me ages to do because I was overthinking the recursion. Once it clicked, I had a huge ‘duh’ moment. Anyhow, here’s what I came up with. I probably could have simplified the ‘hundreds’ part of it, but this was clear in my mind.

def english_number number
  ones  = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
  teens = ['ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'ninteen']
  tens  = ['ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety']
  
  readout = ''
  working = number.to_i
  
  # The idea here... create the variable m_count to check if the input number is over def over 
  # one million. If it is, recurse m_count (which will be less than 1000) through the method
  # to generate the number of millions. Then, tack " million " onto the end of it and change 
  # the working number to what it originally was, minus the number of millions. Then create
  # the variable t_count to check the number of thousands in the remainder and repeat the process.
  # Once the remainder is under 1000, we do not recurse again. You can easily expand this for billions
  # and higher numbers by copying the case for millions, creating a new 'billions' variable, then
  # pulling the same trick and tacking " billion " onto the end of the number returned.
  
  if working > 999999999
    puts 'Enter a number under 1 billion.'
    return
  end
  
  # Case for millions
  m_count = working / 1000000
  if m_count >0
    millions = english_number m_count
    readout = readout + millions + ' million'
    working = working - (m_count * 1000000)
    
    if working > 0
      readout = readout + ' '
    end
    
  end
  
  # Case for thousands
  t_count = working / 1000
  if t_count >0
    thousands = english_number t_count
    readout = readout + thousands + ' thousand'
    working = working - (t_count * 1000)
    
    if working > 0
      readout = readout + ' '
    end
    
  end
  
  # Case for hundreds
  h_count = working / 100
  if h_count > 0
    hundreds = ones[h_count - 1]
    readout = readout + hundreds + ' hundred'
    working = working - (h_count * 100)
    
    if working > 0
      readout = readout + ' '
    end
    
  end
  
  # Case for over 20 and less than 100
  if working < 100 and working >= 20
    ten_count = working / 10
    working = working - (ten_count * 10)
    readout = readout + tens[ten_count -1]
    
    if working > 0
      readout = readout + '-'
    end
    
  end
  
  # Case for teens
  if working >= 10 and working < 20
    working = working - 10
    readout = readout + teens[working]
    working = 0 # so that we do not say twelvetwo, etc.
  end
  
  # Case for singles
  if working > 0 and working < 10
    readout = readout + ones[working -1]
  end
  
  if readout == '' and working == 0
    readout = 'zero'
  end
  
  readout
  
end

puts 'Enter a number: '
entered = gets.chomp

puts english_number(entered)
Advertisements

Written by Clay

October 12, 2009 at 19:43

Posted in Code, Ruby

Tagged with , ,

One Response

Subscribe to comments with RSS.

  1. Ooh! I made a mistake when I first published this and did not set the working number to 0 after calling a ‘teen’ number… the result was ‘seventeenseven’ or ‘thirteenthree,’ for example. I added a line to set working to 0 to alleviate the problem.

    Clay

    October 14, 2009 at 01:48


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: