注入文字Rickrolls


13

因为rickrolling是地球上最大的模因,所以您[知道规则,我也是如此]是要编写最短的代码,从而可以在文本上对不知情的读者进行rickroll。让[you down]有一个包含字母,标点和空格的输入文本。每当前两个节中的[撒谎和伤害您]短语出现在文本中时...

We're no strangers to love
You know the rules and so do I
A full commitment's what I'm thinking of
You wouldn't get this from any other guy
I just wanna tell you how I'm feeling
Gotta make you understand

Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you

...然后将其余行插入方括号中。

输入值

输入是仅包含可打印ASCII和可选尾随换行符的单行字符串。

输出量

输出是单行字符串。每当一组单词(定义为在空格中分隔的输入字符串)与上面的歌词行中的一组单词匹配时,请将行中的其余单词插入字符串中,并用方括号括起来。

附加说明:

这是代码高尔夫球,最少字节获胜。您可以编写程序或函数。

  • 匹配应该不区分大小写:即使在歌词中大写,也要we're转换为。 we're [no strangers to love]We're
  • 匹配应该是贪婪的。 Does he know the answer?应该转换为Does he know the [rules and so do I] answer?而不是Does he know [the rules and so do I] the [rules and so do I] answer?
  • 如果单词在提供的歌词中不止一次出现,请选择任何出现的单词来完成该行。
  • 如果单词是歌词行中的最后一个单词,请不要在其后插入任何内容。
  • 标点符号是“单词”的一部分。 I'm是一个单词,不能与匹配I。同样,you.由于句号的原因,它与任何歌词都不匹配。

有些词I在整个歌词中以及在一行结尾处会多次出现。由于规则是可以使用上述歌词中的任何出现,并且这些出现之一在行的末尾,因此无需进行匹配I。的另一个选项I[just wanna tell you how I'm feeling]

如果两个匹配项重叠,则可以选择其中一个。这意味着how I'm thinking可能成为how I'm [feeling] thinking [of]OR,how [I'm feeling] I'm thinking [of]因为I'm可能是how I'mor的一部分I'm thinking。但是,如果输入文本很简单I'm thinking,则输出应为I'm thinking [of]

其他测试用例:

I don't know what I'm doing with my life.
is converted to
I [just wanna tell you how I'm feeling] don't know [the rules and so do I] what I'm [thinking of] doing with my life.
Many additional solutions are possible, since words like `I` appear multiple times.


Will someone please save me from these memes?
is converted to
Will someone please save me from [any other guy] these memes?


Two strangers walked into a bar.  One said "hello."  The other said "goodbye."
is converted to
Two strangers [to love] walked into a [lie and hurt you] bar.  One said "hello."  The [rules and so do I] other [guy] said "goodbye."

挑战受到了这个家伙的启发。


11
ಠ_ಠ
亚历A.

第二个测试用例似乎是错误的(from应该变为from [any other guy])。
Doorknob

Answers:


6

gawk,316 + 377 = 693

第一个命令行参数是歌词的文件名(375字节+ 2用于调用= 377)。滚动所有其他文件。打印到stdout

BEGIN{FPAT="[^ ]+ *";OFS=""}func d(a){b=tolower(a);sub(/ *$/,"",b);return b}FNR==NR{for(s=$0;NF;$0=s=$0){for(i=1;i<NF;i++){k=k $i;$i="";v[d(k)]="["$0"] "}$0=s;k=$1=""}next}{for(s=$0;NF;$0=s=$0){for(j=NF;(--j)>0&&!(d($0) in v);$(j+1)="");k=v[d($0)];if($0!~/ $/)k=" "k;printf($0 k);for($0=s;j-->=0;$(j+2)="");}print""}

不打高尔夫球

BEGIN{FPAT="[^ ]+ *";OFS=""}
func d(a){b=tolower(a);sub(/ *$/,"",b);return b}
FNR==NR{
  for(s=$0;NF;$0=s=$0){
    for(i=1;i<NF;i++)
    {
      k=k $i;
      $i="";
      v[d(k)]="["$0"] "
    }
    $0=s;
    k=$1=""
  }
  next
}
{
  for(s=$0;NF;$0=s=$0){
    for(j=NF;(--j)>0&&!(d($0) in v);$(j+1)="");
    k=v[d($0)];
    if($0!~/ $/)k=" "k;
    printf($0 k);
    for($0=s;j-->=0;$(j+2)="");
  }
  print""
}

检测结果

输入:

we're
We're
Does he know the answer?
I
how I'm thinking
I'm thinking
I don't know what I'm doing with my life.
Will someone please save me from these memes?
Two strangers walked into a bar.  One said "hello."  The other said "goodbye."
gonna run

输出:

we're [no strangers to love] 
We're [no strangers to love] 
Does he know the [rules and so do I] answer? 
I [just wanna tell you how I'm feeling] 
how I'm [feeling] thinking [of] 
I'm thinking [of] 
I [just wanna tell you how I'm feeling] don't know [the rules and so do I] what I'm [thinking of] doing with my life. 
Will someone please save me from [any other guy] these memes? 
Two strangers [to love] walked into a [lie and hurt you] bar.  One said "hello."  The [rules and so do I] other [guy] said "goodbye." 
gonna run [around and desert you] 

1.这使第一个测试用例失败(它在what和之间放置了东西I'm,这是“非贪婪”匹配)。2.这使第三个测试用例失败(它将两个空格变为一个)。3.如果将歌词存储在单独的文件中,则需要awk在字节数中计算该文件(以及在命令中包括该文件的+2 )。
门把手

@Doorknob谢谢。贪婪是固定的。歌词文件未更改,并且未在挑战开始之前公开可用,因此不会添加到我的角色计数中。我也不算awk的源代码。
Rainer P.

现在,这对于输入gonna run(期望的输出gonna run [around and desert you],实际的输出gonna [tell a lie and hurt you] run [around and desert you])失败。它还仍然连续砍掉多个空格。您需要在字节数中计算歌词文件,因为这是awk程序的非标准调用。
Doorknob

@Doorknob好。我重新实现了大多数程序,gawk现在需要保留空间的字段分隔。现在通过所有测试用例。
Rainer P.

令人印象深刻。应对这一挑战非常困难。
门把手
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.