Legend says phones used to have keyscitation needed and in those times, a person could "encode" a phone number by creating a word from the letters in the keys with the given numbers.
Below is the usual layout of the keys (that matter for my question) of a phone:
| abc | def |
1 | 2 | 3 |
------------------
ghi | jkl | mno |
4 | 5 | 6 |
------------------
pqrs | tuv | wxyz|
7 | 8 | 9 |
------------------
I have to write a function that, given a vector of characters with uppercase letters/digits, recovers the original phone number.
For example,
'HELLO' -> 4 3 5 5 6
'IAMYY4U' -> 4 2 6 9 9 4 8
'' -> ⍬
This is the code I have written:
Telephone ← {
⍝ Monadic function taking an uppercase string as input.
⍝ Decodes the number expressed with uppercase letters,
⍝ e.g. '1ABCZ' gives '12229'
⍝ Split letters and digits according to their keys (each digit goes to itself)
keyStarts ← 'ADGJMPTW'
splits ← ⎕D, keyStarts
classes ← (1 -⍨ ⍳10), 1 + ⍳8
classes[splits ⍸ ⍵]
}
I am particularly interested in a better way to create the classes
variable, that looks like this 0 1 2 3 4 5 6 7 8 9 2 3 4 5 6 7 8 9
and this ordering depends obviously on the order of splits
.
This comes from a problem from an APL competition.