29
\$\begingroup\$

Problem:

You are making a new phone where people can type in specialized phone numbers, for example, 1-800-program, and they would be converted automatically to a usable phone number, like 1-800-7764726 (for the previous example).

Your program will recieve a string if any length with numbers, letters and dashes, and convert all the letters to their corresponding numbers.

Here is a keypad, for reference:

keypad

Rules:

  • Your program will receive a string
  • It will process it and return/print another string
  • Any language is accepted
  • Since it is , the shortest code wins
\$\endgroup\$
2
  • \$\begingroup\$ Should the program handle both upper and lower case letters in the input? \$\endgroup\$ Feb 19, 2014 at 0:38
  • 3
    \$\begingroup\$ @mattnewport - no, assume the variable has been turned into lowercase already \$\endgroup\$
    – TheDoctor
    Feb 19, 2014 at 3:36

41 Answers 41

1
2
0
\$\begingroup\$

Python

import string          
trans = str.maketrans(string.ascii_lowercase,
                      '22233344455566677778889999')                                                                                         
print("1-800-ask-usps".translate(trans))
\$\endgroup\$
0
\$\begingroup\$

ECMASCRIPT, 101 (with input)

"1-800-PROGRAM".replace(/./g,function(c){
return "22233344455566677778889999"[c.charCodeAt(0)-65]||c})

Newline added for clarity. 85 characters if the input is in a variable.

\$\endgroup\$
0
\$\begingroup\$

QBasic, 155

Ah, the memories...

INPUT n$
FOR i=1 TO LEN(n$)
c$=MID$(n$,i,1)
a=ASC(c$)
IF 97>a THEN
PRINT c$;
ELSE IF 122>a THEN
PRINT STR$(a\3.2-28);
ELSE
PRINT 9;
END IF
NEXT i

This should have been shorter, but I was testing with repl.it, which doesn't allow single-line IF statements and behaves strangely if you leave the variable off of NEXT i. It also doesn't recognize the ASC function, so to run the code you'll need to add this workaround at the beginning:

DECLARE FUNCTION ASC(s$)
FUNCTION ASC(s$)
FOR j=1 TO 255
IF CHR$(j)=LEFT$(s$,1) THEN
ASC=j
END IF
NEXT j
END FUNCTION

(The second time you run it, the interpreter will complain unless you remove the DECLARE FUNCTION line, go figure.)

\$\endgroup\$
0
\$\begingroup\$

R, 110

s=strsplit(scan(,""),"")[[1]];i=grep("[a-z]",s);s[i]=sort(c(1:24%%8+2,7,9))[match(s[i],letters)];cat(s,sep="")

Example:

> s=strsplit(scan(,""),"")[[1]];i=grep("[a-z]",s);s[i]=sort(c(1:24%%8+2,7,9))[match(s[i],letters)];cat(s,sep="")
1: 1-800-program
2: 
Read 1 item
1-800-7764726
\$\endgroup\$
0
\$\begingroup\$

VB.net (61c)

Excludes the 45c min for a valid vb.net program.

Module P
Sub Main(A()As string)
For Each x In A(0).ToUpper 
Console.Write(If(x<"A"or x>"Z",x,Chr(Asc(x)\4+35)))
Next
End Sub
End Module
\$\endgroup\$
0
0
\$\begingroup\$

Rust, 196 bytes

x.iter().map(|x|match x{'a'|'b'|'c'=>'2','d'|'e'|'f'=>'3','g'|'h'|'i'=>'4','j'|'k'|'l'=>'5','m'|'n'|'o'=>'6','p'|'q'|'r'|'s'=>'7','t'|'u'|'v'=>'8','w'|'x'|'y'|'z'=>'9',_=>*x}).collect::<Vec<_>>();

Try it online!

Power of match. Yet match for every letter feels unnecessarily tedious. Still trying to grasp iter() and subsequent functions

\$\endgroup\$
0
\$\begingroup\$

brainfuck, 326 bytes

,[>+[-[---<]>>-]<--<<<[>>>>+<[->[-]>+<<]>[-<<+>>]>[-<<+>>]<<-<<+<-]+>>[<<->[-]>->[<+>+]<->>+++++[>+++++<-]>[<+>-]+<<<[>>-<+<-]>[<+>-]>[>-<[-]]>[<<<->>>-]++++[>++++<-]>+<<<<[>>+>>[-<<[-]>+>]<<[-<+>]>[->+<]>-<<<<<+>-]>>>>[-]<<<[<<->>-]<+++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<]>[-]>[-]+++++[>++++++++++<-]>.[-]<<]<<[->.>>[-]<<<]>[-]<,]

Try it online!

For each character:

  • Check if it's a letter. If not, print it and go to the next character.
  • Check if it's 'z'. If it is, subtract 1 to make it 'y'
  • Check if it's after 'r'. If it is, subtract 1 to realign it with the number grid.
  • Divide the letter number by 3.
  • Add the result to 50 ('2') and print it.
stage0 memory layout:
| input x | input backup | compare result z | compare val y | compare t0 | compare t1 |
stage1:
| else flag | input backup | prev compare result / letter index y | inv letter index / compare t0 | compare t1 | compare val x |
stage2:
| else flag | letter index backup | letter index x | compare result z | compare t0 | compare t1 | compare val y |

,[ Read first character
    >+[-[---<]>>-]<-- Check if input is greater than 96 (a lowercase letter)
    <<<[>>>>+
        <[->[-]>+<<]
        >[-<<+>>]
        >[-<<+>>]
        <<-<<+<-
    ]
    
    + Set else flag
    >>[ If input is a letter:
        ((stage1))
        <<->[-]>- Clear the result/else flags
        >[<+>+]<- Invert the letter index

        >>+++++[>+++++<-]> Check if letter 25 (z)
        [<+>-]+
        <<<[>>-<+<-]
        >[<+>-]
        >[>-<[-]]
        >[<<<->>>-] If it is subtract 1 to change it to y

        ((stage2))
        ++++[>++++<-]>+ Check if greater than letter 17 (r)
        <<<<[>>+
            >>[-<<[-]>+>]
            <<[-<+>]
            >[->+<]
            >-<<<<<+>-
        ]
        >>>>[-] Clear y
        <<<[<<->>-] If it is subtract 1 to normalize indices
        <+++<[->-[>+>>]>[+[-<+>]>+>>]<<<<<] Divide normalized index by 3
        >[-]>[-] Clear tempvars from division
        +++++[>++++++++++<-]>. Add 50 ('2') to result and print it
        [-]<< Clear letter index

        ((stage0))
    ]<<[ If input isn't a letter:
        - Clear the result flag
        >. Just print the character
        >>[-]<<< Clear the letter index
    ]
    >[-]<, Next input
]
\$\endgroup\$
0
\$\begingroup\$

Retina, 27 bytes

T`l`22233344455566677778889

Try it online!

Port of the Bash solution. Transliterates each lowercase letter to a digit between 2 and 9. The list is implicitly padded with 3 additional 9s

\$\endgroup\$
0
\$\begingroup\$

Haskell, 80 bytes

d c|elem c['a'..'z']="22233344455566677778889999"!!(fromEnum c-97)|1>0=c
l=map d

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Thunno 2, 11 bytes

Ạż6+kP÷T9vṆ

Try it online!

Explanation

Ạż6+kP÷T9vṆ  # Implicit input
Ạ            # Push the lowercase alphabet
 ż           # Without popping, push [1..26]
  6+         # Add 6 to make it [7..26]
    kP÷      # Floor divide each by pi
       T9v   # Replace the 10 with a 9
          Ṇ  # Transliterate the input
             # Implicit output
\$\endgroup\$
-1
\$\begingroup\$

Python 3, 130 bytes

d=lambda x:str(int((ord(x)-96)*.31769+1.76715))
def f(s,c=''):
	for i in s:c+=(d(i)if i!='z'else'9')if i.isalpha()else i
	print(c)

Try it online!

Uses the formula y = (x * .31769) + 1.76715 to approximate the given relation. Sadly this formula gives wring value for z hence requires one additional check

\$\endgroup\$
2
  • \$\begingroup\$ 80 \$\endgroup\$ May 29, 2022 at 5:38
  • 1
    \$\begingroup\$ 65 based on the above \$\endgroup\$
    – emanresu A
    May 29, 2022 at 5:50
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.