键盘代码到文本!


14

给定一个字符串和一个数组作为输入,您的任务是输出在典型的移动键盘上键入时输入字符串将打印的文本。在移动键盘中,通过按n次按钮来键入字母,其中n是字母在按钮标签上的位置。因此,22应输出b

键盘


规则

  • 辅助数组将包含字符映射表([" ",".,!","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"])。这将为您节省一些字节。

  • #符号将切换大小写。初始大小写会更低。所以2#3应该输出aD

  • 0会添加一个空格。因此,202应输出a a

  • 输入的字符串中将有一个空格(),以在同一数字按钮上开始一个新字母。例如,键入时aa,输入String为2 2

  • 保证输入的String始终是有效的键盘代码。


输入值

您可以使用您的语言支持的任何方式进行输入。


输出量

您可以按任何方式输出结果。功能return也是允许的。


测试用例

#4440555#666888330#999#66688111 -> "I Love You!"
#6#33777 7779990#222#4477744477778627777111 -> "Merry Christmas!"
#44#27 79990#66#3390#999#332777111 -> "Happy New Year!"


这是,因此以字节为单位的最短代码胜出!



4
我认为year在最后一个测试案例中大写是错误的。
Maltysen '16

1
我们是否需要考虑循环?像2222-> invalid还是2222-> b?
李奎林

@Maltysen是的,你是对的。我已经编辑了问题。感谢您指出。:)
Arjun

出于兴趣,是否##需要处理或加倍空间?
尼尔,

Answers:



3

JavaScript,105 99字节

f=
(s,a)=>s.replace(/#| ?((.)\2*)/g,(m,n,d)=>d?(l=a[d][n.length-1],x?l:l.toUpperCase()):(x=!x,''),x=1)

a=['  ','.,!','abc','def','ghi','jkl','mno','pqrs','tuv','wxyz']

F=s=>console.log( f(s,a) )
F('#4440555#666888330#999#66688111')
F('#6#33777 7779990#222#4477744477778627777111');
F('#44#27 79990#66#3390#999#332777111');

  • 感谢@Neil节省6个字节。

您可以通过将字母存储在临时文件(例如l)中,然后使用来节省一些字节c?l:l.toUpperCase()
尼尔,

@尼尔 考虑到数组已经是小写了……,谢谢:)
Washington Guedes

2

Perl 6的 119  97字节

基于地图的解决方案119字节

->$_,\a{my$u=0;[~] map {/'#'/??{$u+^=1;|()}()!!(&lc,&uc)[$u](a[.substr(0,1)].substr(.chars-1,1))},.comb(/(\d)$0*|'#'/)}

尝试一下

基于替代的解决方案97字节

->$_,\a{my$u=0;S:g/(\d)$0*|./{$0??(&lc,&uc)[$u](a[$0].substr($/.chars-1,1))!!($u+^=$/eq'#')x 0}/}

尝试一下

展开:

->     # pointy block lambda

  $_,  # input string
  \a   # helper array

{

  my $u = 0;

  S                        # substitute (implicit against 「$_」)
  :global
  /

    | (\d) $0*             # digit followed by same digit
    | .                    # everything else

  /{

    $0                     # is 「$0」 set (digit)


    ??                     # if so then
        (&lc,&uc)[$u](     # call either 「lc」 or 「uc」

          a[$0]            # get the value from the input array
          .substr(         # select the correct character
            $/.chars - 1,
            1
          )

        )


    !!
        (
          $u +^= $/ eq '#' # numeric xor $u if 「#」 was matched
        ) x 0              # string repeated zero times (empty string)

  }/
}

2

JavaScript ES6-124字节

打高尔夫球:

f=h=>a=>(o=c="")+a.match(/#|(.)\1*/g).forEach(e=>e==" "?0:e=="#"?c=!c:(n=h[e[0]][e.length-1])*(o+=c?n.toUpperCase():n))?o:0;

f=h=>a=>(o=c="")+a.match(/#|(.)\1*/g).forEach(e=>e==" "?0:e=="#"?c=!c:(n=h[e[0]][e.length-1])*(o+=c?n.toUpperCase():n))?o:0;

console.log(f(["  ",".,!","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"])("#4440555#666888330#999#66688111"));
console.log(f(["  ",".,!","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"])("#6#33777 7779990#222#4477744477778627777111"));
console.log(f(["  ",".,!","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"])("#44#27 79990#66#3390999332777111"));

取消高尔夫:

f=(a,h)=>{
    //out string
    o="";
    //uppercase or lowercase (initialized as "" and then inverted in golfed version)
    c=0;
    //split it into array of instructions, which are sets of repeated characters, or # solely alone
    a.match(/#|(.)\1*/g).forEach((e)=>{
        e==" "?0:
            e=="#" ? (c=!c) : ( ()=>{ //lambda added because two statements ungolfed, multiplied in the golfed version
                    n=h[e[0]][e.length-1];
                    o+=c?n.toUpperCase():n;
                })()
    })
    return o;
}

1

JavaScript,301字节

(a,b)=>{u="l";p=[];r="";a.split``.map((c,i)=>p.push(c!=a[i-1]?" "+c:c));p.join``.trim().replace('   ', ' ').split` `.map(l=>{if(l=="#"){u=(u=="l"?b.forEach((y,j)=>b[j]=y.toUpperCase())||"u":b.forEach((y,j)=>b[j]=y.toLowerCase())||"l")}else{if(l!="  "){r+=b[+l[0]][l.length-1]}else{r+=" "}}});return r}

f=(a,b)=>{u="l";p=[];r="";a.split``.map((c,i)=>p.push(c!=a[i-1]?" "+c:c));p.join``.trim().replace('   ', ' ').split` `.map(l=>{if(l=="#"){u=(u=="l"?b.forEach((y,j)=>b[j]=y.toUpperCase())||"u":b.forEach((y,j)=>b[j]=y.toLowerCase())||"l")}else{if(l!="  "){r+=b[+l[0]][l.length-1]}else{r+=" "}}});return r}

console.log(f("#4440555#666888330#999#66688111 ",["  ",".,!","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"]));
console.log(f("#6#33777 7779990#222#4477744477778627777111",["  ",".,!","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"]));
console.log(f("#44#27 79990#66#3390#999#332777111",["  ",".,!","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"]));

我知道这很长,但这是我能做到的。


1

V,60字节

Í /|
ͨ䩨±*©/½a[submatch(1)][len(submatch(2))]
Í|
ò/#
g~$x

(有一个无法打印的½<Ctrl+r>a

在线尝试!

说明


Í /|                                          #Replace all " " with "|"
ͨ䩨±*©                                      #Replace all (\d)(\1*)
        /½                                    #With =
          ^Ra                                 #(Inserts the passed array)
             [submatch(1)][len(submatch(2))]  #Index into the array
Í|                                            #Replace all "|" with "" (second ò implied)
ò   ò                                         #Recursively (until breaking)
 /#                                           #Go to the next #
g~$                                           #Toggle case until the of the line
   x                                          #Delete current char (#)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.