## Algorithmathon Day 17: Telephone Words

Created: 26 August 2013  Modified:

It is easier for most humans to remember words rather than to remember numbers. Having only 10 numbers and 26 letters the numbers would need to double or triple up with letters. The history of the telephone keypad is interesting. For example it wasn’t until mobile telephones became ubiquitous that the world was even using the same letters for the same numbers. The results of this world standard makes little since to me. We will view the correlations between letters and numbers in our code.

Our challenge will be to write a small program to list out the possible combinations of letters for a telephone number. I think of this problem as being a series of wheels with letters on the circumference. If each wheel represents a number then some wheels will have no letters on it while some will have three or even four letters. The first wheel starts spinning and when it reaches the first letter it starts spinning the the second wheel which starts spinning the third wheel and so on. For a seven digit number the cost could range from 7 to 4^7^. The first being the case for the number 111-1111 and the second being 999-9999. Most numbers will fall towards the middle of these two extremes.

A solution will involve steps similar to the below.

• Read the first/next letter and place it in a stack
• If there is another number go to step 1.
• Remove the last letter from the stack
• Go to step 2

Seems pretty straightforward let’s hop right into build our RSpec tests.

telephone_words_spec.rb

Finally our solution.

telephone_words.rb

tags: Algorithm - Algorithmathon - RSpec - Ruby - Telephone Words
Less Is More