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.
![]()
Input
"Example"
Output
"339926755533"
Note
Rules
The signature of the contest entry MUST be:
Class codeGolf.MultiTap Extends %RegisteredObject { ClassMethod ToKeyPad(phrase) As %String { Return "" // Your solution here } }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).
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) }The use of
$ZWPACKand$ZWBPACKis also discouraged.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
Before posting the code, let's post the score 馃槉
Mine, so far, is 177
Ops! I assumed that punctuation includes blank, but looking at the test cases, it's not!
So mine is 186
Little optimization: 181
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...
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
}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>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 ?
p is ok, quit is ok.
Things like:
ClassMethod ToKeyPad(phrase = {some code to do preprocessing}) As%String
{
Return phrase
}are not okay.
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
}More optimization: 171
165
160
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
}
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
}Reduced your code to 127:
ClassMethod ToKeyPad(p) As %String
{
a s l=$f(" 0 1 ABC2 DEF3 GHI4 JKL5 MNO6 PQRS7TUV8 WXYZ9",$$$UPPER($e(p,$i(i)))) q:l=1 r s:l $p(r,l-2\5,*+l-2#5+1)="" g a
}My try was this :
ClassMethod ToKeyPad(p = "E") As %String
{
s t=$ZCVT($TR($g(p),"`~!@#$%^&*()_+{}][:;'.,<>/?",""),"U"),s=" 0^1^ABC2^DEF3^GHI4^JKL5^MNO6^PQRS7^TUV8^WXYZ9" f i=1:1:$l(t){s 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)_d} q r
}
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
Your code does not take into account the numbers, so an incorrect result is given for "L33T C0d3r".