language-agnosticcode-golfrosetta-stone

Code Golf: Phone Number to Words


Guidelines for code-golf on SO

We've all seen phone numbers that are put into words: 1-800-BUY-MORE, etc.

What is the shortest amount of code you can write that will produce all the possible combinations of words for a 7 digit US phone number.

Input will be a seven-digit integer (or string, if that is simpler), and assume that the input is properly formed.

Output will be a list of seven-character strings that

For instance, the number 428-5246 would produce

GATJAGM
GATJAGN
GATJAGO
GATJAHM
GATJAHN
GATJAGO
GATJAIM
GATJAIN
GATJAIO and so on.....

Winning criterion will be code from any language with the fewest characters that produces every possible letter combination.

Additional Notes:

Bonus points awarded for recognizing output as real English words. Okay, not really. ;-)

Added: Okay, lets go with "Nick's Modified North American Classic Key Pad" which has an 'O' (oh, not zero) on the 6 key.


Solution

  • q, 52 39 chars

    (cross/)(3 cut"--- ",.Q.A except"QZ")

    q is evaluated left of right.

    .Q.A contains the vector "ABC...XYZ"

    except returns x excluding values in y

    join "--- " to the modified char vector

    cut into chunks of 3

    cross returns all possible combinations, and is applied across all chunks

    q)(cross/)(3 cut"---   ",.Q.A except"QZ")[4 2 8 5 2 4 6] 
    "GATJAGM"
    "GATJAGN"
    "GATJAGO"
    "GATJAHM"
    "GATJAHN"
    "GATJAHO"
    "GATJAIM"
    "GATJAIN"
    "GATJAIO"
    ....