星期一迷你高尔夫#5:<s>不要</ s>不要在家尝试


24

星期一迷你高尔夫:每周一发布(希望!)一系列简短的问题。

有时人们会厌倦生活规则:“不要这样做”,“你不能那样做”,“我们不会让你那样做”。有时似乎确实有限制!但是,时不时地获得一些乐趣是一件好事,所以让我们编写一些代码来修改这些规则。而在我们讨论的同时,也可能会修改其他负面因素。(当然,这些修改将不会只是暂时的,因此我们也将保留原始措辞。)

挑战

您面临的挑战是编写一个程序或函数,以将HTML <s>删除线置于</s>限制性词(即n'tnot该词结尾或之后的词)周围,并在每一个词之后将其正当数插入所有大写字母中。最后,在空格后,应包括更换的数量。例如:

Please don't jump into the pool.

变成

Please <s>don't</s> DO jump into the pool. 1

对于以n't或结尾not(以及cannot)结尾的单词,正等价词是指上述所有字符not(不包括空格)。这就是我的意思:

  • do not speak 变成 <s>do not</s> DO speak
  • it doesn't work 变成 it <s>doesn't</s> DOES work
  • we cannot 变成 we <s>cannot</s> CAN

但是,也有一些例外。确保正确处理这些内容。

can't -> <s>can't</s> CAN
won't -> <s>won't</s> WILL
ain't -> <s>ain't</s> AM
shan't -> <s>shan't</s> SHALL
I'm not -> <s>I'm not</s> I AM
you're not -> <s>you're not</s> YOU ARE

细节

  • 输入将不包含任何空格,除了普通空格(没有制表符,换行符等)。
  • 输入将永远不会包含任何双负数(例如we can't not do this)。
  • 如果a not在标点符号后立即出现,或者作为另一个词的一部分出现,则保留它。
  • 确保在<s></s>标签之间保留原始文本,包括大写/小写。
  • 如果愿意,可以使用<strike></strike>代替<s></s>

测试用例

输入:

I'm sorry, but you can't do that.
Driving on the beach isn't allowed.
Driving on the beach is not allowed.
Please don't jump in; I cannot imagine what might come of that.
Don't worry; we won't get into trouble.
I'm not perfect, but you're not either.
You shan't do it 'cause I ain't doin' it!
Can't we capitalize special cases?
I don't like the words can't, shan't, won't, don't, ain't, or ppcgn't.
Oh, this? It's nothing.
Tie a slipknot in the rope.
Would you like Pinot Noir?
This sentence contains none of the replacement words. Not even knot or ca't.
This sentence doesn't contain one of the replacement words.

输出:

I'm sorry, but you <s>can't</s> CAN do that. 1
Driving on the beach <s>isn't</s> IS allowed. 1
Driving on the beach <s>is not</s> IS allowed. 1
Please <s>don't</s> DO jump in; I <s>cannot</s> CAN imagine what might come of that. 2
<s>Don't</s> DO worry; we <s>won't</s> WILL get into trouble. 2
<s>I'm not</s> I AM perfect, but <s>you're not</s> YOU ARE either. 2
You <s>shan't</s> SHALL do it 'cause I <s>ain't</s> AM doin' it! 2
<s>Can't</s> CAN we capitalize special cases? 1
I <s>don't</s> DO like the words <s>can't</s> CAN, <s>shan't</s> SHALL, <s>won't</s> WILL, <s>don't</s> DO, <s>ain't</s> AM, or <s>ppcgn't</s> PPCG. 7
Oh, this? It's nothing. 0
Tie a slipknot in the rope. 0
Would you like Pinot Noir? 0
This sentence contains none of the replacement words. Not even knot or ca't. 0
This sentence <s>doesn't</s> DOES contain one of the replacement words. 1

计分

这是,因此以字节为单位的最短有效代码获胜。Tiebreaker提交的提交首先达到其最终字节数。10月26日下星期一将不会选择获胜者。祝您好运!


@FryAmTheEggman是的,除非还有其他值得添加到列表中的东西。我宁愿简短,但是您有什么建议吗?
ETHproductions

不,只是不想让它遭受“您现在改变了规格,现在很烂”的挑战综合征:P
FryAmTheEggman 2015年

1
建议的测试用例:Can't we capitalize special cases?
DLosc

我们是否需要小心字符串:Oh this? It's nothing...I wonder if we'll notice any words like this?
唐·黑斯廷斯

2
一个<strike> not </ strike>只是将模因转换为比赛!
1

Answers:


7

PIP138 140个字节

Yeesh,那cannot/ knot区别是棘手。

x:"ca wo ai sha i'm you're"^sY"CAN0WILL0AM0SHALL0I AM0YOU ARE"^0OqR-`([\w']+)( no|n'|(?<=can)no)t\b`{++i"<s>".a."</s>".s.((yx@?LCb)|UCb)}s.i

从stdin读取一行,输出到stdout。正则表达式:

`([\w']+)( no|n'|(?<=can)no)t\b`

匹配一个单词(包括撇号),后跟三件事之一:

  • not
  • n't
  • not 如果单词的前半部分是 can

-正则表达式上的运算符使其不区分大小写。

匹配将替换为以下功能的结果。(注意:在函数内,a是整个匹配项,b是捕获组1。)

{++i"<s>".a."</s>".s.((yx@?LCb)|UCb)}
 ++i                                   Increment counter
    "<s>".a."</s>"                     Return entire match wrapped in HTML tags...
                  .s.(             )   plus space, plus the following:
                           LCb         Lowercase first capture group
                        x@?            Find its index in list x of special cases (nil if
                                         not in list)
                      (y      )        Use that as index into list y of replacements
                               |UCb    If it wasn't a special case, this is nil, and we
                                         instead use uppercase(b)

完成修改后的字符串后,我们还将输出一个空格和替换数i


3

GNU Sed,321个字节

(包括的+1 -r

:
s!(can('|no)t)([^<])!<s>\1</s> CAN\3!i
s!(won't)([^<])!<s>\1</s> WILL\2!i
s!(ain't)([^<])!<s>\1</s> AM\2!i
s!(shan't)([^<])!<s>\1</s> SHALL\2!i
s!(I'm not)([^<])!<s>\1</s> I AM\2!i
s!(you're not)([^<])!<s>\1</s> YOU ARE\2!i
t
s!(([^ .!?]+)(n't| not))([^<])!<s>\1</s> \U\2\4!i
t
h
s/1//g
s/<s>/1/g
s/[^1]//g
x
G
s/\n/ /

“字符串替换-sed的工作!” 我想。但这是令人惊讶的困难,而且我一直不停地替换原始文本。还有计数!至少在这个问题上没有禁止一元的问题。


2

Perl,153个字节

150个脚本+ 3个 -p

$c=0|s!\b((ca)nnot|([\w']+)(?: not|n't))\b!"<s>$&</s> ".uc({ai,AM,ca,CAN,wo,WILL,sha,SHALL,"i'm","I AM","you're","YOU ARE"}->{lc$+}||$+)!egi;s/
/ $c
/

因此,尽管我现在匹配了所有测试用例,但是该正则表达式使我付出了沉重的代价……我会考虑的!

输出示例:

$perl -p can.pl <<< "I'm sorry, but you can't do that.
Driving on the beach isn't allowed.
Driving on the beach is not allowed.
Please don't jump in; I cannot imagine what might come of that.
Don't worry; we won't get into trouble.
I'm not perfect, but you're not either.
You shan't do it 'cause I ain't doin' it!
Can't we capitalize special cases?
I don't like the words can't, shan't, won't, don't, ain't, or ppcgn't.
Oh, this? It's nothing.
This sentence contains none of the replacement words. Not even knot or ca't.
This sentence doesn't contain one of the replacement words.
Tie a slipknot in the rope.
Would you like Pinot Noir?
You cannot be serious\!"
I'm sorry, but you <s>can't</s> CAN do that. 1
Driving on the beach <s>isn't</s> IS allowed. 1
Driving on the beach <s>is not</s> IS allowed. 1
Please <s>don't</s> DO jump in; I <s>cannot</s> CAN imagine what might come of that. 2
<s>Don't</s> DO worry; we <s>won't</s> WILL get into trouble. 2
<s>I'm not</s> I AM perfect, but <s>you're not</s> YOU ARE either. 2
You <s>shan't</s> SHALL do it 'cause I <s>ain't</s> AM doin' it! 2
<s>Can't</s> CAN we capitalize special cases? 1
I <s>don't</s> DO like the words <s>can't</s> CAN, <s>shan't</s> SHALL, <s>won't</s> WILL, <s>don't</s> DO, <s>ain't</s> AM, or <s>ppcgn't</s> PPCG. 7
Oh, this? It's nothing. 0
This sentence contains none of the replacement words. Not even knot or ca't. 0
This sentence <s>doesn't</s> DOES contain one of the replacement words. 1
Tie a slipknot in the rope. 0
Would you like Pinot Noir? 0
You <s>cannot</s> CAN be serious\! 1

哇,这很紧凑!一个问题:I'm not工作不正常-应该I AM代替I'M。但我认为你只需要改变I'm,以i'm在代码中修复它。
DLosc

哦,也可以在诸如Tie a slipknot in the rope.或的句子上尝试一下Would you like Pinot Noir?。他们不是在测试用例还,但任择议定书的目的是排除在结尾的任何字not除外cannot
DLosc 2015年

@DLosc优秀的测试用例!我想不出以结尾的单词not!将I'm是一个糟糕的小姐,谢谢你的位置!
唐·黑斯廷斯
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.