Inspired by the Google Code Challenge:
The Latin alphabet contains 26 characters and telephones only have ten digits on the keypad. We would like to make it easier to write a message to your friend using a sequence of keypresses to indicate the desired characters. The letters are mapped onto the digits as shown below. To insert the character B for instance, the program would press 22. In order to insert two characters in sequence from the same key, the user must pause before pressing the key a second time. The space character ' ' should be printed to indicate a pause. For example, 2 2 indicates AA whereas 22 indicates B.
Each message will consist of only lowercase characters a-z and space characters ' '. Pressing zero emits a space.
Your challenge is to write the smallest function which takes the input string, and returns the sequence of keypresses necessary to produce the input as string or output it to stdout. The function which is the least amount of bytes wins.
Example Input/Output
phone("hi")
44 444
phone("hello world")
4433555 555666096667775553
Other clarifications
- Pauses must only be added when necessary and must be a space ' '.
- Each message will consist of only lowercase characters a-z and space characters ' '. Print
0
to signify spaces. - No external libraries.
- Only the input string may be passed to your function.
- To make other languages competitive, the primary function declaration doesn't count, and nor does importing other standard libraries.
#include
s,import
s, andusing
s don't count. Everything else does. This does include#define
s and helper functions. See rule 2 in this question if you are confused. - Multiple spaces can be denoted as
00
or0 0
since you don't really have to pause between a space
{}
a part of the function signature? For example, if my code isfunction f(){alert('hi');}
, should I count the characters ofalert('hi');
or{alert('hi');}
? \$\endgroup\$t9
works differently: you have to click each key once to get a word. \$\endgroup\$