阅读新闻很无聊。帮帮我!


22

每个人都知道这个消息很无聊。真无聊。除非涉及政客及其丑闻。真好玩!但是可惜,政客只能犯下这么多丑闻。因此,我正在雇用您使新闻更加有趣。

目标给定一个HTML代码段,执行在此处找到的所有替换,即在此图片中:

但是您不应该自己编辑任何HTML标记。我将任何在HTML标记或它们的属性中找不到的单词称为有效单词。也就是说,您不应替换<div class="smartphone">iPhone</div><div class="pokedex">iPhone</div>,而应替换<div>election</div><div>eating contest</div>

解析规则

  • 编辑您只能匹配由非单词描述的单词。也就是说,您只能匹配完整的单词。(例如,匹配“您好见证人”,但不匹配“轻率”,“轻率”或“维特尼空间选择”。
  • 如果无法在键盘上键入字符,则应显示最接近的等效字符。(例如,“智能手机”实际上将变成“ Pokedex”)
  • 首字母大写或全字母大写必须保留。(例如,“据称他们死亡”变成“金田很可能已经死亡”,“据称他们死亡”变成“他们大概已经死亡”,“选举!”变成“饮食比赛!”,而“ SpAcE”变成“ Spaaace”和“ nEW STUDY”成为“ tumblr post”)
  • 所有情况都必须匹配。(即,无论大小写,您都必须匹配一个单词。)
  • 满足的任何实例a <vowel>都应成为an <vowel>反之亦然。(例如,“参议员”变成“精灵王”)您不必为每个实例都这样做,但是至少对于您自己的替补而言。
  • 任何复数形式也必须在翻译中保留复数形式(例如,“智能手机”变为“ pokedex”,“空格”变为“ Spaaaces”)
  • 一定时态的任何内容都必须保留在翻译中的那个时态中。(例如,“重建”变为“复仇”,“重建”变为“复仇”,等等。)
  • 任何单数形式都必须在翻译中保持单数形式(例如,“国会领袖”变成“河魂”)
  • 如果条目跨越多个HTML元素,则您仍应将其匹配,但可以“丢弃”所有中间元素。(例如,S<b>pa</b>ace应简单地成为“ spaaace”)。如果条目是独立包含在单个元素中的,则应正确匹配条目并保留标签。
  • “目击者”应该变成“我认识的那些家伙”
  • “目击者”应该变成“我认识的这个家伙”
  • “见证人”应该变成“我认识的那个家伙”
  • “无法联系到评论”应该变成“有罪,每个人都知道”
  • “无法联系到评论”应该变成“有罪并且大家都知道”

奖金

如果您获得-N%奖金,则您的new amount = (old amount) * (1 - N/100)。因此,如果您-5%收到了500字节代码的奖金,则您的new amount = 500 * (1 - 5/100) = 500 * .95 = 475

  • 如果在第一次替换之后为每个“空格”实例添加一个额外的“ a”,则将获得-5%的奖励。因此,第一个替换项是“ spaaace”,第二个替换项是“ spaaaace”,第三个替换项是“ spaaaaace”,依此类推。
  • 如果您为每个有效数字使用相应XKCD漫画的链接替换该数字,则获得-5%的奖励。(它不必还存在)。如果你去这个奖金,你应该匹配的数字,例如5003,0004 523,和5.324。(您可以选择用漫画的图像替换该数字。如果您这样做-5%,则将获得红利而不是赠金-15% bonus-15%如果可以在图片中添加标题文本,则可以得到另一种额外的赠金。)
  • 如果您可以同时替换图像右侧的所有实例和左侧的实例,则可获-25%的奖励。(例如,“空间”变成“空间”,“饮食比赛”变成“选举”,等等。)如果您选择任何奖金,则应该能够将其还原为各自的条目。(例如,http://www.xkcd.com/542应变为“ 542”,而“ spaaaaaaaace”应变为“空间”。)
  • 您可以选择最少进行6次替换,但是对于每个未完成的替换(不包括奖金替换),您将额外收取+ 10%的罚款。

IO示例(无奖金)

Input: Witnesses allegedly used their smartphones to commit the crime.
Output: These dudes I know kinda probably used their pokedex to commit the crime.

Input: Mayor Golfalot rebuilt the city after the fatal election.
Output: Mayor Golfalot avenged the city after the fatal eating contest.

Input: This <b>new study</b> shows that people in <span class="space">space</span> cannot be reached for comment.
Output: This <b>tumblr post</b> shows that people in <span class="space">spaaace</span> are guilty and everyone knows it.

Input: <b>g</b><i>oo</i><s>g</s><u>le</u> glass is terrible. :(
Output: virtual boy is terrible. :(

Input: Electric SMARTPHONES have been shown to be more productive when not used by Senators and when not used in cars.
Output: Atomic POKEDEX have been shown to be more productive when not used by Elf-lords and when not used in cats.

Input: Homeland Security has been established as an evil foundation, especially in space.
Output: Homestar runner has been established as an evil foundation, especially in spaaace.

Input: The congressional leaders are testy today.
Output: The river spirits are testy today.

Input: SPACE is not a Senator or a cAR.
Output: SPAAACE is not an Elf-lord or a cat.

Input: Mr. Biz is running for reelection.
Output: Mr. Biz is running for reelection.

Input: Google   glass !
Output: Virtual boy !

Input: New (or old) study
Output: New (or old) study

这是一个,因此以字节为单位的最短程序获胜。


“ Witnessespace”应成为什么?
lirtosiast 2015年

@ThomasKwa“见证空间”。不要匹配不完整的单词。
Conor O'Brien


3
这看起来是一个有趣的挑战。我很快就会试一试
Sam Weaver 2015年

1
@DomHastings是的,我可以;将没有无效的输入,也没有自动关闭的标签(例如<br/>
Conor O'Brien

Answers:


8

Perl 5、850

使用或生成了大量正则表达式。
%l哈希用于重复的单词。

%l=qw(A avenge B _be_reached_for_comment C could D dude E pokedex G google_glass I _I_know K river_spirit L congressional_leader P smartphone 4 rebuil N new_stud T tumblr_post V virtual_boy W witness Y _guilty_and_everyone_knows_it);$t="G,V;Ges,Vs;Ps,E;P,E;4d,A;4t,Ad;Nies,Ts;Ny,T;Wes,these DsI;W,this DI;allegedly,kinda probably;cannotB,areY;car,cat;cars,cats;Ls,Ks;L,K;C notB,isY;Cn'tB,isY;election,eating contest;electric,atomic;homeland Security,homestar runner;senator,elf-lord;senators,elf-lords;space,spaaace";$o=$s=$_;$s=~s/\s\s*/ /g;map{$t=~s/$_/$l{$_}/g}keys%l;$t=~s/_/ /g;@L=split/;/,$t;map{my@T=split/,/;push@W,\@T}@L;map{$e=$a=$W[$_][0];$b=$W[$_][1];$U=uc$a;$u=ucfirst$a;$s=~s/(?<![\w"])$U(?![\w"])/\U$b/g;$s=~s/(?<![\w"])$u(?![\w"])/\u$b/g;$s=~s/(?<![\w"])$a(?![\w"])/$b/gi;$e=~s@.@(<.*?>)?$&(</.*?>)?@g;$s=~s/$e /$b /ig;}0..@W;$_=$s.$/

测试

$ cat news.txt |perl -p readingnews.pl
These dudes I know kinda probably used their pokedex to commit the crime.
Mayor Golfalot avenged the city after the fatal eating contest.
This <b>tumblr post</b> shows that people in <span class="space">spaaace</span> are guilty and everyone knows it.
virtual boy is terrible. :(
Atomic POKEDEX have been shown to be more productive when not used by Elf-lords and when not used in cats.
Homestar runner has been established as an evil foundation, especially in spaaace.
The river spirits are testy today.
SPAAACE is not a Elf-lord or a cat.
Mr. Biz is running for reelection.
Virtual boy !
New (or old) study

恭喜第一!
Conor O'Brien 2015年

9

JavaScript ES6,954个字节

我认为这将比以前容易得多!我最初使用JavaScript,所以我可以轻松地仅进入文本节点并在其中替换文本,但这不是问题的出处,所以我就成了这种怪异!它通过所有测试用例,但由于标签变得平衡<b>g</b><i>oo</i><s>g</s><u>le</u> glass is terrible. :(,整个字符串返回<b>标签中。代码的大部分是查找表,我可能仍然缺少一些边缘情况,但是我无法提出一种更好的方法来正确处理时态,我敢肯定我可以对其进行更多压缩,但是我的想法已经用完了...我确定稍后再讲。应该使用Perl,更好的选择@LukStorms

d=document.createElement`div`,u='toUpperCase'
d[i='innerHTML']=prompt``;`the ${w='witness'}es,those dudes I know|${w}es,these dudes I know|the ${w},this dude I know|${w},dude I know|allegedly,kinda probably|new study,tumblr post|new studies,tumblr posts|rebuild,avenge|rebuilt,avenged|space,spaaace|${g='google glass'},virtual boy|${g}es,virtual boys|smartphones,pokédex|smartphone,pokédex|electric,atomic|senator,elf-lord|car,cat|election,eating contest|congressional leader,river spirit|homeland security,homestar runner|could not ${b='be reached for comment'},is${g=' guilty and everyone knows it'}|couldn't be reached for comment,is${g}|cannot be reached for comment,are${g}`[v='split']`|`.map(w=>d[i]=d[i][r='replace'](eval(`/(<[^>]+)?\\b${(w=w[v]`,`)[0][v]``.join`(?:<\\/?[^>]+>)*`[r](/ /g,'\\s+')}(s?)\\b/gi`),(s,q,t)=>[q?s:((!s.match(/[^A-Z]/)?w[1][u]():s.match(/^[A-Z]/)?w[1][r](/^./,q=>q[u]()):w[1])+t),q?0:w[1]=w[1][r](/ce$/,'ace')][0])[r](/\ba(?= [aeiou])/gi,s=>s=="a"?"an":"AN"))
alert(d[i])

我申请一个奖金的5%spaaace得到额外a每个成功匹配后。alert()@sysreq建议使用,谢谢!

例子

Input: <b>g</b><i>oo</i><s>g</s><u>le</u> glass is terrible. :(
Output: <b>virtual boy is terrible. :(</b>
Input: space. Space? Space! SPACE!
Output: spaaace. Spaaaace? Spaaaaace! SPAAAAAACE!
Input: Smartphones aren't really smart phones, but: SMARTPHONES!
Output: Pokédex aren't really smart phones, but: POKÉDEX!
Input: Senator John Doe was arrested today after attempting to write a new study on the danger of smartphones being used in the car, on his smartphone whilst driving his car. A witness testified to having seen the senator committing the crime. When questioned, Senator Doe "could not be reached for comment". It's unknown if his reputation can be rebuilt and this puts new doubts on the upcoming election. Congressional leaders have yet to comment.
Output: Elf-lord John Doe was arrested today after attempting to write a tumblr post on the danger of pokédex being used in the cat, on his pokédex whilst driving his cat. A dude I know testified to having seen the elf-lord committing the crime. When questioned, Elf-lord Doe "is guilty and everyone knows it". It's unknown if his reputation can be avenged and this puts new doubts on the upcoming eating contest. River spirits have yet to comment.

奖励:书签

在控制台中运行此命令以使正文文本就地更新:

d=document.body;`the ${w='witness'}es,those dudes I know|${w}es,these dudes I know|the ${w},this dude I know|${w},dude I know|allegedly,kinda probably|new study,tumblr post|new studies,tumblr posts|rebuild,avenge|rebuilt,avenged|space,spaaace|${g='google glass'},virtual boy|${g}es,virtual boys|smartphones,pokédex|smartphone,pokédex|electric,atomic|senator,elf-lord|car,cat|election,eating contest|congressional leader,river spirit|homeland security,homestar runner|could not ${b='be reached for comment'},is${g=' guilty and everyone knows it'}|couldn't be reached for comment,is${g}|cannot be reached for comment,are${g}`[v='split']`|`.map(w=>d[i='innerHTML']=d[i][r='replace'](eval('/(<[^>]+)?\\b'+(w=w[v]`,`)[0][v]``.join`(?:<\\/?[^>]+>)*`[r](/ /g,'\\s+')+'(s)?\\b/gi'),(s,q,t)=>[q?s:((s.match(/^[A-Z]+$/)?w[1].toUpperCase():s.match(/^[A-Z]/)?w[1][r](/^./,q=>q.toUpperCase()):w[1])+(t||"")),q?0:w[1]=w[1][r](/ce$/,'ace')][0])[r](/\ba ([aeiou])/gi,(s,t)=>s[0]=="a"?"an "+t:"AN "+t))

2
alertconsole.log在浏览器中运行的时间短。

<b>标签的“问题”是完全可以接受的,如果不是更多的接受。我喜欢最后一个例子。我正在尝试考虑将其中大多数纳入其中的一种。恭喜第一个答案!我确定它具有竞争力;)
Conor O'Brien 2015年
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.