1-800-PPRAXIS
September 18, 2018
I hate businesses with telephone numbers that spell words; it’s awkward to type letters at a telephone, and often the words are abbreviated or spelled wrong so they are no longer mnemonic, which defeats the purpose. So this morning when I had to call one of those businesses, I wrote a program to translate letters to numbers after I had been waiting on hold long enough to get really annoyed.
Your task is to write a program to translate telephone numbers specified as words. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Here’s a Haskell version. We keep any non-letters.
Python version. As with Globules, I kept any non-letters.
Outputs as follows:
Here’s a solution in C.
Example:
Here’s a code golf solution in Python.
Output:
Solution in ruby.
All three version output:
input: 1-800-PPRAXIS
output: 1-800-7772947
pass: ✅
input: +1-514-AUTOBUS
output: +1-514-2886287
pass: ✅
input: 13TAXI
output: 138294
pass: ✅
input: 1800-THRIFTY
output: 1800-8474389
pass: ✅
input: 1-US-RENT-A-WRECK
output: 1-87-7368-2-97325
pass: ✅
Tests from @Globules
A small improvement to Alex B’s solution is to make use of the Python dict’s ability to return a default when it can’t find a key: