Montag Mini-Golf # 5: <s> nicht </ s> DO versuchen, dies zu Hause

24

Montag Minigolf: Eine Reihe von kurzen Fragen, die (hoffentlich!) Jeden Montag gestellt werden.

Manchmal haben die Leute die Lebensregeln satt: "Tu das nicht", "Das kannst du nicht", "Wir lassen dich das nicht machen". Es kann manchmal wirklich einschränkend wirken! Aber hin und wieder ist es gut, ein bisschen Spaß zu haben. Schreiben wir also Code, um diese Regeln zu ändern. Und wenn wir schon dabei sind, könnten auch andere Negativität ändern auch. (Natürlich werden diese Änderungen nicht nur vorübergehend sein, daher behalten wir auch den ursprünglichen Wortlaut bei.)

Herausforderung

Ihre Herausforderung besteht darin, ein Programm oder eine Funktion zu schreiben, mit der restriktive Wörter, <s>dh</s> Wörter, die auf ALL CAPS enden n'toder notvon diesen gefolgt werden , durchgestrichen werden. Am Ende, nach einem Raum, machte die Anzahl des Ersatzes sollte einbezogen werden. Beispielsweise:

Please don't jump into the pool.

wird

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

Bei Wörtern, die auf (sowie ) enden n'toder von not(sowie cannot) gefolgt werden , ist das positive Äquivalent alles bis zu den oben genannten not(ohne Leerzeichen). Folgendes meine ich:

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

Es gibt jedoch einige Ausnahmen. Stellen Sie sicher, dass diese richtig behandelt werden.

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

Einzelheiten

  • Die Eingabe enthält nie Leerzeichen außer normalen Räumen (ohne Tabs, Zeilenumbrüche, etc.).
  • Die Eingabe enthält niemals doppelte Negative (zB we can't not do this).
  • Wenn eine not unmittelbar nach einem Satzzeichen angezeigt wird , oder als Teil eines anderen Wortes, lassen Sie es sein.
  • Achten Sie darauf, den Originaltext, einschließlich Groß- / Kleinschreibung, zwischen den <s></s>Tags beizubehalten.
  • Wenn Sie möchten, können Sie <strike></strike>anstelle von verwenden <s></s>.

Testfälle

Eingänge:

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.

Ausgänge:

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

Wertung

Das ist , also gewinnt der kürzeste gültige Code in Bytes. Tiebreaker geht zu einer Übermittlung über, die als erste ihre letzte Byteanzahl erreicht hat. Der Gewinner wird am kommenden Montag, den 26. Oktober, nicht ermittelt. Viel Glück!

ETHproductions
quelle
@FryAmTheEggman Ja, es sei denn, es gibt andere, die es wert sind, zur Liste hinzugefügt zu werden. Ich möchte mich lieber kurz fassen, aber haben Sie Vorschläge?
ETHproductions
Nein, nur nicht das von der leiden wollen „Sie die Spezifikation jetzt verändert es saugt“ challenge - Syndrom: P
FryAmTheEggman
1
Vorgeschlagener Testfall:Can't we capitalize special cases?
DLosc 20.10.15
Müssen wir vorsichtig sein mit Zeichenketten wie: Oh this? It's nothing...oder I wonder if we'll notice any words like this??
Dom Hastings
2
Ein <strike> nicht </ strike> wandelt Memes einfach in Wettbewerbe um!
Jan

Antworten:

7

Pip , 138 140 Bytes

Ja, das cannot/ knotUnterscheidung ist schwierig.

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

Liest eine Zeile von stdin und gibt sie an stdout aus. Der Regex:

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

Stimmt mit einem Wort (einschließlich Apostrophen) überein, gefolgt von einem von drei Dingen:

  • not
  • n't
  • not ohne ein Leerzeichen, vorausgesetzt, die erste Hälfte des Wortes war can

Das - Operator eines regulären Ausdrucks unterscheidet nicht zwischen Groß- und Kleinschreibung.

Übereinstimmungen werden durch das Ergebnis der folgenden Funktion ersetzt. (Hinweis: innerhalb der Funktion aist die gesamte Übereinstimmung und bist Erfassungsgruppe 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)

Sobald die geänderte Zeichenfolge fertig ist, geben wir auch ein Leerzeichen und die Anzahl der Ersetzungen aus i.

DLosc
quelle
3

GNU Sed, 321 Bytes

(einschließlich für +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/ /

"String Ersatz - ein Job für sed!" Ich dachte. Aber das war überraschend schwierig, und ich habe mich immer wieder mit der Ersetzung des Originaltextes befasst. Und das Zählen! Zumindest gibt es in der Frage kein Unary-Verbot ...

Toby Speight
quelle
2

Perl, 153 Bytes

150 Skript + 3 für -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
/

Also, während ich jetzt alle Testfälle abgleiche, aber diese Regex hat mich teuer gekostet ... Ich werde darüber nachdenken!

Beispielausgabe:

$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
Dom Hastings
quelle
Wow, das ist kompakt! Ein Problem: I'm notfunktioniert nicht richtig - es sollte I AMstatt I'M. Aber ich denke, Sie müssen nur den Code ändern I'm, i'mum das Problem zu beheben.
DLosc
Versuchen Sie es auch mit Sätzen wie Tie a slipknot in the rope.oder Would you like Pinot Noir?. Sie befinden sich noch nicht in den Testfällen, aber die Absicht des OP war es, alle Wörter auszuschließen, die mit notexcept enden cannot.
DLosc
@ DLosc Ausgezeichnete Testfälle! Ich konnte mir keine weiteren Wörter vorstellen, die auf enden not! Das I'mwar ein schlimmer Fehlschlag, danke für den Spot!
Dom Hastings