查找最短的Swype路径


12

介绍

最近,我已经习惯于使用Swype打字。

我注意到可以通过从起始字母到结束字母画一条直线,或者跳过重复的字母来产生某些单词。

例如,我可以balloon通过扫过以下字母来输入单词:

b> a> l> o> n

挑战

让我们将“ 最短Swype路径”(或)定义SSP为键入字符串所需的最小可区分线段数。线段是任意两个或多个字母之间的连续直线。方向的任何变化都会开始一个新的线段-尽管某些单词可能仅通过绘制一条直线而被Swyped。

使用以下简单的QWERTY键盘布局:

q w e r t y u i o p
a s d f g h j k l
  z x c v b n m

在上面的示例中,单词balloon将具有SSP4如以下序列中所述:

1) Start at `b` (line segments = 0)
2) slide to `a` (line segments = 1)
3) slide to `l` (line segments = 2)
4) slide to `o` (line segments = 3)
5) slide to `n` (line segments = 4)

字符串qwerty具有SSP= 1,因为切换该单词时不需要改变方向。

输入值

单个字符串,包含a-z通过STDIN,函数参数或命令行中的任何一个。

输出量

通过STDOUT打印,返回或使用您的语言最接近的替代品打印,该数字n表示字符串的SSP

outut中有一个尾随换行符可选。不允许出现标准漏洞。以字节为单位的最短提交获胜。

笔记

  • 方向改变会开始一个新的线段。
  • 重复的字母仅计数一次(例如:bookkeeper应视为bokeper)。
  • 通常,Swpye通过查看相邻字母并填充其最佳猜测来纠正丢失的字母。对于此挑战,假设没有自然语言增强,预测性文本或错误纠正。
  • 大写A-Z输入被视为小写输入。
  • 忽略0-9输入中的任何数字。
  • 对角线路径允许-即,直线覆盖字母okn,例如,计为1段。此规则适用于任何斜纹(例如:字母chi都行)。

例子

Input          Output
---------------------
a                0
aa               0
aaaaaa           0
aaaaaabc         2
in               1
int              2
java             3
qwerty           1
chicago          5
balloon          4
BALLOON          4
typewriter       5
bookkeeper       6
stackexchange    11
2hello7          3
2HELLO7          3

h在从c到i的直线上,但是c和o之间没有字母?
Sparr

如果提交的内容还必须支持大写字母,我建议在测试用例中包括一些字母。
丹尼斯

@Sparr正确。
CzarMatt 2015年

@Dennis好话-添加了测试用例。
CzarMatt 2015年

我确实喜欢用Swype打字,但是我不知道为线路编程...
mbomb007 2015年

Answers:


8

CJam,78 76 73 68 62字节

relA,s-:_2ew{:-},{{i"x8LÑejPG ÀÏi"225b28b=A+Ab}/.-:ma}%e`,

请注意,该代码包含不可打印的字符。

借用@isaacg的使用RLE计数路径的巧妙想法节省了6个字节。

CJam解释器中在线尝试。如果该链接无效,请复制此粘贴中的代码。

怎么运行的

rel      e# Read a token from STDIN and cast it to lowercase.
A,s-     e# Remove all digits.
:_       e# Duplicate each element of the argument (string/array).
2ew      e# Push all overlapping slices of length 2.
{:-},    e# Filter out slices where both elements are equal.
{        e# For each slice:
  {      e#   For each character of the slice:
    i    e#     Push its code point.

    "x8LÑejPG ÀÏi"225b28b

         e#     Convert the string from base 225 to base 28, pushing the array
         e#     [15 7 16 17 18 27 26 8 9 0 3 11 4 6 24 1 22 5 21 10 25 23 12 2 13 14].
         e#     These are the positions on the QWERTY keyboard, starting from the
         e#     upper left corner and going right.

    =    e#     Select the element that corresponds to the code point.

         e#     Arrays wrap around in CJam. Since 'a' has code point 97, the array has
         e#     length 26 and 97 % 26 == 19, 'a' corresponds to the 20th element.

    A+Ab e#     Add 10 and convert to base 10. Example: 8 -> [1 8]
  }/     e#
  .-     e#     Vectorized subtraction. [a b] [c d] -> [a-c b-d]
  :ma    e#     atan2. Pushes the angle of the direction. [y x] -> arctan(y/x)
}%       e#
e`       e# Apply run-length encoding.
,        e# Count the runs.

非常聪明。谢谢你的解释。
CzarMatt 2015年

3

Pyth,53 50 49字节

lrPMfT-VJm.jF.D@jC"XÖ;¿ìÇ×;¤ð$  _"28CdT@GrzZtJ8

键盘压缩格式归功于@Dennis。

该答案包含一些不可打印的字符。请参阅下面的链接以获取正确的代码。

示范测试线束。

说明:

lrPMfT-VJm.jF.D@jC"..."28CdT@GrzZtJ8
                              rzZ      Convert input to lowercase.
                            @G         Take the intersection with G.
                                       This removes the numbers.
         m                             Map over the characters
                 C"..."                Treat the string as a base 256 number.
                j      28              Convert this number to base 28, giving:
                                       [15, 7, 16, 17, 18, 27, 26,  ... ]
                                       which is the character positions 
                                       from top left, rotated by 97 places.
               @         Cd            Index this list by ord(character)
             .D            T           divmod by 10, giving the x-y position.
          .jF                          Convert this to a complex number.
        J                              Save the result to J.
      -VJ                        tJ    Vectorize - over J and J[1:]. This gives
                                       pairwise diffeences between J's elements.
    fT                                 Filter the differences on being truthy.
                                       This removes letter pairs with no movement.
  PM                                   Map the remaining complex numbers to their
                                       phases, which indicates their directions.
 r                                 8   Run-length encode the result.
l                                      Print out the number of separate RLE groups.

短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.