Integer#to_word

The other day some friends and I came across a job posting for ITA Software on the T. It read:

If the integers from 1 to 999,999,999 are written as a word, and concatenated, what is the 51 billionth letter?

The eight of us spent the duration of the ride discussing this question and its solutions. This inspired me to write Integer#to_word for Ruby, as part of one possible way to solve it.

From the tutorial:

> 999999.to_word
=> "nine hundred ninety nine thousand nine hundred ninety nine"
Advertisements

4 Comments

  1. Posted May 21, 2007 at 12:02 am | Permalink

    Neat. Suggested extension:

    class Range
    def to_word
    return to_s unless first.respond_to?(:to_word)
    “#{first.to_word} to #{last.to_word}” + (exclude_end? ? ” exclusive” + “”)
    end
    end
    end

    > (1..100).to_word
    => one to one hundred
    > (1…10).to_word
    => one to ten exclusive

  2. Posted May 21, 2007 at 12:09 am | Permalink

    Oh cool, I didn’t know about exclusive ranges.

  3. Abhinav
    Posted October 29, 2007 at 1:59 pm | Permalink

    Doesn’t the problem ask to sort the number-words alphabetically?

  4. Posted October 29, 2007 at 2:15 pm | Permalink

    I remember it as ordered numerically, but alphabetically could also be correct.

    Regardless, this code could help for a brute-force method.


%d bloggers like this: