Written by

Senior Cloud Architect at InterSystems
Discussion Eduard Lebedyuk 路 Jan 26, 2024

Code Golf: Multi-Tap technique

Multi-tap or multi-press is the name given to the historic technique of writing SMS on the first mobile phones with a keyboard of 10-12 numeric keys. For example, to type LOL you need to press 5 three times, 6 three times and 5 three times again. Your task is to write a function that takes a string as input and returns the repeated digits associated with each character according to the multi-tap system. Should consider letters from A to Z, doesn't distinguish between upper/lowercase characters. Numbers are allowed, and any punctuation should be ignored.

multitap system

Input

"Example"

Output

"339926755533"

Note

Rules

  1. The signature of the contest entry MUST be:

    Class codeGolf.MultiTap Extends %RegisteredObject
    {
        ClassMethod ToKeyPad(phrase) As %String
        {
            Return "" // Your solution here
        }
    
    }
    
  2. It is forbidden to modify class/signature, including but not limited to:

    • Adding inheritance
    • Setting default argument values
    • Adding class elements (Parameters, Methods, Includes, etc).
  3. It is forbidden to refer to non-system code from your entry. For example, this is not a valid entry:

    ClassMethod Build(f As %Integer)
    {
      W ##class(myPackage.myClass).test(a)
    }
    
  4. The use of $ZWPACK and $ZWBPACK is also discouraged.

  5. You can use this test case:

    Class codeGolf.unittest.MultiTap Extends %UnitTest.TestCase
    {
        Method TestMultiTap()
        {
            Do $$$AssertEquals(##class(codeGolf.MultiTap).ToKeyPad("Example"), "339926755533")
            Do $$$AssertEquals(##class(codeGolf.MultiTap).ToKeyPad("Dumbphone TEXT"), "38862274466666330833998")
            Do $$$AssertEquals(##class(codeGolf.MultiTap).ToKeyPad("InterSystems IRIS"), "44466833777777799977778336777704447774447777")
            Do $$$AssertEquals(##class(codeGolf.MultiTap).ToKeyPad("Code Golf"), "22266633304666555333")
            Do $$$AssertEquals(##class(codeGolf.MultiTap).ToKeyPad("L33T C0d3r"), "55533333333802220033333777")
        }
    }
    

Comments

Enrico Parisi 路 Jan 26, 2024

Before posting the code, let's post the score 馃槉

Mine, so far, is 177

0
Enrico Parisi  Jan 26, 2024 to Enrico Parisi

Ops! I assumed that punctuation includes blank, but looking at the test cases, it's not!

So mine is 186

0
Enrico Parisi  Jan 26, 2024 to Enrico Parisi

Little optimization: 181

0
Roger Merchberger 路 Jan 26, 2024

I can't get the test case code to work (maybe because I only have access to 2017.2...) but manually executing the tests like this:

w##class(CodeGolf.MultiTap).ToKeyPad("L33T C0d3r")="55533333333802220033333777"

and verifying that the output was '1' instead of '0', my code does pass the test cases.

I was able to get a length of 173...

0
Roger Merchberger  Jan 29, 2024 to Roger Merchberger

Anyway, here's my solution - maybe it'll help someone make theirs even shorter.

ClassMethod ToKeyPad(phrase) As%String
{
 SQ=" 0^1^ABC2^DEF3^GHI4^JKL5^MNO6^PQRS7^TUV8^WXYZ9",P=$ZCVT(phrase,"U") F I=1:1:$L(P){S C=$E(P,I) F J=1:1:10{SK=$F($P(Q,"^",J),C) F L=1:1:K-1SX=$G(X)_(J-1)}} ReturnX
}
0
Vitaliy Serdtsev  Jan 29, 2024 to Roger Merchberger

Reduced your code to 137:

<FONT COLOR="#000080">ClassMethod聽</FONT><FONT COLOR="#000000">ToKeyPad(</FONT><FONT COLOR="#ff00ff">p</FONT><FONT COLOR="#000000">)聽</FONT><FONT COLOR="#000080">As聽%String
</FONT><FONT COLOR="#000000">{
聽</FONT><FONT COLOR="#0000ff">f聽</FONT><FONT COLOR="#800000">i</FONT><FONT COLOR="#000000">=1:1:</FONT><FONT COLOR="#0000ff">$l</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">p</FONT><FONT COLOR="#000000">)</FONT><FONT COLOR="#800080">{</FONT><FONT COLOR="#0000ff">f聽</FONT><FONT COLOR="#800000">j</FONT><FONT COLOR="#000000">=0:1:9</FONT><FONT COLOR="#800080">{</FONT><FONT COLOR="#0000ff">f聽</FONT><FONT COLOR="#800000">l</FONT><FONT COLOR="#000000">=1:1:</FONT><FONT COLOR="#0000ff">$f</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#0000ff">$p</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008000">"聽0^1^ABC2^DEF3^GHI4^JKL5^MNO6^PQRS7^TUV8^WXYZ9"</FONT><FONT COLOR="#000000">,</FONT><FONT COLOR="#008000">"^"</FONT><FONT COLOR="#000000">,</FONT><FONT COLOR="#800000">j</FONT><FONT COLOR="#000000">+1),</FONT><FONT COLOR="#0000ff">$e</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#0000ff">$$$UPPER</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">p</FONT><FONT COLOR="#000000">),</FONT><FONT COLOR="#800000">i</FONT><FONT COLOR="#000000">))-1聽</FONT><FONT COLOR="#0000ff">s聽</FONT><FONT COLOR="#800000">r</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#0000ff">$g</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">r</FONT><FONT COLOR="#000000">)_</FONT><FONT COLOR="#800000">j</FONT><FONT COLOR="#800080">}}聽</FONT><FONT COLOR="#0000ff">q聽</FONT><FONT COLOR="#800000">r
</FONT><FONT COLOR="#000000">}</FONT>
0
Enrico Parisi  Jan 29, 2024 to Vitaliy Serdtsev

Hem....from the main post:

"The signature of the contest entry MUST be:".....

Class codeGolf.MultiTap Extends%RegisteredObject
{
    ClassMethod ToKeyPad(phrase) As%String
    {
        Return""// Your solution here
    }

}

Is using a different variable name and quint instead of return allowed ?

0
Eduard Lebedyuk  Jan 29, 2024 to Enrico Parisi

p is ok, quit is ok.

Things like:

ClassMethod ToKeyPad(phrase = {some code to do preprocessing}) As%String
    {
        Return phrase
    }

are not okay.

0
Christian Berger 路 Jan 26, 2024

207... 馃槖

197 if we can get rid of phrase and change return to short quit

ClassMethod ToKeyPad(phrase) As%String
{
	S t=$ZCVT(phrase,"U") F i=2:1:9 {F j=1:1:$S("79"[i:4,1:3) S b($C($I(s)+64))=i_j,b(i)=i_j+1} Sr="" F i=1:1:$L(t) {S a=$E(t,i) S:a=" "r=r_0 S:"01"[a r=r_a_a F j=1:1:$E($G(b(a)),*) Sr=r_$E(b(a))} returnr
}
0
Enrico Parisi 路 Jan 29, 2024

Here is mine, 160

ClassMethod ToKeyPad(phrase) As%String
{
	sx=$$$UPPER($zstrip(phrase,"*P",," ")) f i=1:1:$l(x){s p=$f("1****ABC2*DEF3*GHI4*JKL5*MNO6*PQRS7TUV8*WXYZ9 0",$e(x,i)),$p(r,p+3\5#10,*+p-2#5+1)=""} returnr
}
0
Enrico Parisi 路 Jan 29, 2024

131

ClassMethod ToKeyPad(y) As%String
{
	f i=1:1:$l(y){s p=$f(" 0   1    ABC2 DEF3 GHI4 JKL5 MNO6 PQRS7TUV8 WXYZ9",$e($$$UPPER(y),i)) s:p $p(r,p+3\5-1,*+p-2#5+1)=""} qr
}
0
Vitaliy Serdtsev  Jan 30, 2024 to Enrico Parisi
 

Reduced your code to 127:

ClassMethod ToKeyPad(pAs %String
{
l=$f(" 0   1    ABC2 DEF3 GHI4 JKL5 MNO6 PQRS7TUV8 WXYZ9",$$$UPPER($e(p,$i(i)))) q:l=1 s:$p(r,l-2\5,*+l-2#5+1)="" a
}
0
Sundara Moorthy Jeganathan 路 Jan 30, 2024

My try was this :

ClassMethod ToKeyPad(= "E") As %String
{
t=$ZCVT($TR($g(p),"`~!@#$%^&*()_+{}][:;'.,<>/?",""),"U"),s=" 0^1^ABC2^DEF3^GHI4^JKL5^MNO6^PQRS7^TUV8^WXYZ9" i=1:1:$l(t){a=$e(t,i),b=$f(s,a)-1,c=$l($E(s,1,b),"^"),d="",$p(d,c-1,$f($p(s,"^",c),a))="",r=$g(r)_dr
}

0
Timo Lindenschmid 路 Feb 7, 2024
ClassMethod MultiTap(t) As%String
{
 f{s f=$f(" _ADGJMPTW,__BEHKNQUX,__CFILORVY,_______S_Z",$e($$$UPPER(t),$i(i)))-1q:f=0s$p(e,f#11-1,*+(f\11+1))=""} q e
}

Size 123

0
Vitaliy Serdtsev  Feb 8, 2024 to Timo Lindenschmid

Your code does not take into account the numbers, so an incorrect result is given for "L33T C0d3r".

0