跨键盘走


21

给定一个单词(或任何字母序列)作为输入,您必须在每个字母之间进行插值,以使结果中的每个相邻字母对在QWERTY键盘上也相邻,就像您在巨型键盘上行走时键入输入一样。例如,“ ”可能会变成“ y tr es ”,“ ”可能会变成“ c xz a wer t ”。

规则:

  • 这是您应该使用的键盘格式:

    qwertyuiop
    asdfghjkl
      zxcvbnm

    在此布局中触摸的任何一对键都被认为是相邻的。例如,“ s”和“ e”是相邻的,但“ s”和“ r”不是。

  • 输入的“单词”将由任何字母序列组成。它只有字母,所以您不必处理特殊字符。
  • 输入可以采用任何方便的形式:stdin,字符串,列表等。您可以选择任何更方便的方式。
  • 输出可以采用任何方便的形式:stdout,字符串,列表等。字母大小写无关紧要,并且不需要保持一致。
  • 键盘上的任何路径都是有效的,除了不能再越过上一个字母才能到达下一个字母。例如,“ hi ”可以变成“ h j i ”或“ h jnbgyu i ”,但不能变成“ h b h u i ”。
  • 字母本身不相邻,因此“ poll ”不能成为“ poll ”。相反,它将需要变成类似“ pol k l ”的东西。
  • 单词之前或之后不允许输出字母。例如,“ was ”不能成为“ tre was ”或“ was dfg”。

这是代码高尔夫,最短的答案以字节为单位。


因此,我们要为每个输入输出任何有效的“遍历”吗?给定两个输入,确定是否是一个有效的步步,似乎会更好。
维斯卡

似乎dewqwerty是的有效路径dy。你能确认吗?
Arnauld

@Arnauld是的。
Vaelus

@Veskah是的;输出任何有效的步数作为输入。这是为了进行优化,例如,如果它必须走的最短,则可能无法实现。
Vaelus

Answers:


6

Japt -g,23个字节

;D·ÎÔ+D·Årí)pUl)fUq".*?

在线尝试!

将输入作为大写字母数组。否则与其他答案非常相似。

说明:

;                          :Set D to the keyboard layout
 D·Î                       :Get the first row of keys
    Ô                      :Reversed
     +                     :Concat
      D·Å                  :The other two rows
         rí)               :Interleaved
            p              :Repeat that string
             Ul)           : A number of times equal to the length of the input
                f          :Get the substrings that match
                 U         : The input
                  q".*?    : joined with ".*?"
                           :Implicitly output just once of the matches

14

Python 2,83个字节

lambda s:re.findall('.*?'.join(s),'qwertyuioplkmnjhbvgfcxdsza'*len(s))[0]
import re

在线尝试!

遍历整个键盘,直到输入单词为止。


2
为什么import re在代码之后而不是之前呢?
BruceWayne

@BruceWayne re.findall当lambda运行时将对其进行评估,因此在lambda的定义之后可以导入。话虽这么说,进口之前更加清晰,没有必要
普希金

@pushkin啊,我不知道感谢您的澄清!您是根据个人喜好/选择而导入还是完全有助于字节计数?
BruceWayne

2
@BruceWayne这个论坛有点约定俗成。只是它与TiO网站组织代码的方式一起工作。尝试点击“在线试用!” 链接,以了解我的意思。
mypetlion

8

Python 2 2,274字节(最佳解决方案)

296 300 302 308 315 319 324 327 328 430 432字节

-4个字节归功于mypetlion

from networkx import*
i=input()
M,z='qwertyuiop  asdfghjkl   zxcvbnm'.center(55),i[:1]
G=from_edgelist([(M[e],M[e+h])for h in[-1,1,11,12,-11,-12]for e in range(44)if' '!=M[e]and' '!=M[e+h]])
for y,x in zip(i,i[1:]):z+=[shortest_path(G,y,x)[1:],list(G[y])[0]+y][x==y]
print z

在线尝试!

该解决方案可提供最短的输出。键盘被转换为用于查找最短路径以计算输出字符串的图形:

puzzles     --> poiuhbvcxzazxcvbhjklkiuytres
programming --> poiuytrtyuioijhgtresasdcvbnmkmkijnbg
code        --> cvbhjioijhgfde
golf        --> ghjiolkjhgf
yes         --> ytres
hi          --> hji
poll        --> polpl

274个字节:在线尝试!
mypetlion

1
@mypetlion你做了一个重要的减少,你可以更新答案:)
mdahmoune

4

JavaScript(ES6),70个字节

与TFeld相同的策略。

s=>'qazsxdcfvgbhnjmklpoiuytrew'.repeat(s.length).match(s.join`.*?`)[0]

在线尝试!


3

05AB1E,43 个字节

ü)Jε©žVćRs`.ιJ«D«Œʒg≠yн®нQyθ®θQ**}yªн¨}JIθ«

这不是正确的语言,因为它不能像其他答案一样使用正则表达式。

在线尝试验证所有测试用例

说明:

ü)               # Split the input into overlapping pairs
                 #  i.e. "poll" → ["p","o"],["o","l"],["l","l"]]
  J              # Join each inner list together
                 #  i.e. ["p","o"],["o","l"],["l","l"]] → ["po","ol","ll"]
   ε             # Map each to:
    ©            #  Store the current value in the register
    žV           #  Push ["qwertyuiop","asdfghjkl","zxcvbnm"]
    ćR           #  Extract the head, and reverse it
                 #   i.e. ["qwertyuiop","asdfghjkl","zxcvbnm"] → "poiuytrewq"
    s`           #  Swap to take the remainder, and push them to the stack
               #  And then interweave them with each other
                 #   i.e. ["asdfghjkl","zxcvbnm"]
                 #    → ["a","z","s","x","d","c","f","v","g","b","h","n","j","m","k","l"]
        J        #  Join the list to a single string
                 #   i.e. → "azsxdcfvgbhnjmkl"
         «       #  Merge them together
                 #   i.e. "qwertyuiop" and "azsxdcfvgbhnjmkl"
                 #    → "poiuytrewqazsxdcfvgbhnjmkl"
          D«     #  Duplicate it, and append it to itself
                 #   i.e. "poiuytrewqazsxdcfvgbhnjmkl"
                 #    → "poiuytrewqazsxdcfvgbhnjmklpoiuytrewqazsxdcfvgbhnjmkl"
            Π   #  Get all substrings of this strings
                 #   i.e. → ["p","po","poi",...,"k","kl","l"]
ʒ              } #  Filter this list by:
 g              #   Where the length is NOT 1 (otherwise pair "ll" would result in "l")
              *  #   and
   yн®нQ         #   Where the first character of the substring and pair are the same
             *   #   and
        yθ®θQ    #   Where the last character of the substring and pair are the same
                 #    i.e. "po" → []
                 #    i.e. "ll" → ["lazsxdcfvgbhnjmkl"]
yª               #  After filtering, append the current pair to the filtered list
                 #   i.e. [] → ["po"]
                 #   i.e. ["lazsxdcfvgbhnjmkl"] → ["lazsxdcfvgbhnjmkl","ll"]
  н              #  Get the first item
                 #   ["po"] → "po"
                 #   ["lazsxdcfvgbhnjmkl","ll"] → "lazsxdcfvgbhnjmkl"
   ¨             #  Remove the last character
                 #   i.e. "po" → "p"
                 #   i.e. "lazsxdcfvgbhnjmkl" → "lazsxdcfvgbhnjmk"
}                # Close the map
 J               # Join everything together
                 #  i.e. ["p","o","lazsxdcfvgbhnjmk"] → "polazsxdcfvgbhnjmk"
  Iθ«            # And append the last character of the input
                 # (and output the result implicitly)
                 #  i.e. "polazsxdcfvgbhnjmk" and "poll" → "polazsxdcfvgbhnjmkl"

3

木炭,48字节

≔”&⌈″⌊5EWXVNa…-εW¶ζR”η≔⌕η§θ⁰ζFθF⊕﹪⁻⌕ηιζ²⁶«§ηζ≦⊕ζ

在线尝试!链接是详细版本的代码。说明:

≔”&⌈″⌊5EWXVNa…-εW¶ζR”η

获取字符串qwertyuioplkmjnhbgvfcdxsza

≔⌕η§θ⁰ζ

找到单词第一个字符的位置。该索引通常在刚到达的字符后一个索引,但是此值伪造循环的第一次迭代以打印单词的第一个字符。

Fθ

循环每个字符。

F⊕﹪⁻⌕ηιζ²⁶«

计算要打印的字符数以包含单词的下一个字符,然后循环多次。

§ηζ≦⊕ζ

打印循环索引的下一个字符并增加索引。


您是否尝试过旋转字符串“ qwertyuioplkmjnhbgvfcdxsza”并查看是否有任何旋转更可压缩?我不熟悉木炭的压缩。也许这是不可能的。
Vaelus

@Vaelus我也不知道,所以我尝试了所有26次旋转,但是它们都压缩为20个字节。当然,这些并不是所有可能的散步……
尼尔
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.