Lunes Mini-Golf # 5: <s> No </s> SÍ intente esto en casa

24

Minigolf de lunes: una serie de preguntas breves de , publicadas (¡con suerte!) Todos los lunes

A veces la gente se cansa de las reglas de la vida: "no hagas esto", "no puedes hacer eso", "no te dejaremos hacer esto". ¡Puede parecer realmente restrictivo a veces! Pero de vez en cuando, es bueno divertirse un poco, así que escribamos un código para modificar estas reglas. Y mientras estamos en eso, también podríamos modificar otra negatividad. (Por supuesto, estas modificaciones NO SERÁN solo temporales, por lo que también dejaremos la redacción original).

Reto

Su desafío es escribir un programa o función que ponga <s>tachados de HTML </s>alrededor de palabras restrictivas, es decir, palabras que terminen en n'to seguidas de, noty después de cada una de ellas, inserte su equivalente positivo en TODAS LAS MAYÚSCULAS. Al final, después de un espacio, se debe incluir el número de reemplazos realizados. Por ejemplo:

Please don't jump into the pool.

se convierte

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

Para las palabras que terminan en n'to seguidas por not(así como cannot), el equivalente positivo es todo hasta lo mencionado anteriormente not(excluyendo espacios). Esto es lo que quiero decir:

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

Sin embargo, hay algunas excepciones. Asegúrese de que estos se manejen adecuadamente.

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

Detalles

  • La entrada nunca contendrá espacios en blanco, excepto espacios normales (sin pestañas, líneas nuevas, etc.).
  • La entrada nunca contendrá ningún negativo doble (p we can't not do this. Ej .).
  • Si notaparece inmediatamente después de un signo de puntuación, o como parte de otra palabra, déjelo.
  • Asegúrese de conservar el texto original, incluidas las mayúsculas y minúsculas, entre las <s></s>etiquetas.
  • Si lo desea, puede usarlo <strike></strike>en lugar de <s></s>.

Casos de prueba

Entradas:

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.

Salidas:

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

Tanteo

Este es el , por lo que gana el código válido más corto en bytes. Tiebreaker va a la presentación que alcanzó su conteo final de bytes primero. El ganador no será elegido el próximo lunes 26 de octubre. ¡Buena suerte!

ETHproducciones
fuente
@FryAmTheEggman Sí, a menos que haya otros que valga la pena agregar a la lista. Prefiero que sea breve, pero ¿tienes alguna sugerencia?
ETHproductions
No, simplemente no quería que esto sufriera el síndrome de desafío "cambiaste las especificaciones ahora que apesta": P
FryAmTheEggman
1
Caso de prueba sugerido:Can't we capitalize special cases?
DLosc
¿Debemos tener cuidado con cadenas como: Oh this? It's nothing...o I wonder if we'll notice any words like this??
Dom Hastings
2
¡Un <strike> no </strike> simplemente convierte memes en concursos!
Jan

Respuestas:

7

Pip , 138 140 bytes

Yeesh, que cannot/ knotdistinción es difícil.

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

Lee una línea desde stdin, emite a stdout. La expresión regular:

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

coincide con una palabra (incluidos los apóstrofes) seguida de una de tres cosas:

  • not
  • n't
  • not sin espacio, siempre que la primera mitad de la palabra fuera can

El -operador en una expresión regular hace que no distinga entre mayúsculas y minúsculas.

Las coincidencias se reemplazan con el resultado de la siguiente función. (Nota: dentro de la función, aes todo el partido y bes el grupo de captura 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)

Una vez que la cadena modificada está hecha, también mostramos un espacio y el número de reemplazos i.

DLosc
fuente
3

GNU Sed, 321 bytes

(incluido +1 para -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/ /

"Reemplazo de cuerdas: ¡un trabajo para sed!" Pensé. Pero esto fue sorprendentemente difícil, y seguí avanzando con la sustitución del texto original. Y el conteo! Al menos no hay prohibición de unario en la pregunta ...

Toby Speight
fuente
2

Perl, 153 bytes

150 script + 3 para -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
/

Entonces, aunque coincido con todos los casos de prueba ahora, pero esa expresión regular me ha costado mucho ... ¡Lo pensaré!

Salida de ejemplo:

$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
fuente
¡Guau, esto es compacto! Un problema: I'm notno funciona correctamente, debería serlo en I AMlugar de hacerlo I'M. Pero creo que sólo tiene que cambiar I'ma i'men el código para solucionarlo.
DLosc
Ah, también, pruébalo en oraciones como Tie a slipknot in the rope.o Would you like Pinot Noir?. Todavía no están en los casos de prueba, pero la intención del OP era excluir cualquier palabra que terminara en notexcepto cannot.
DLosc
@DLosc ¡Excelentes casos de prueba! ¡No podía pensar en palabras más largas que terminaran not! El I'mfue un mal señorita, gracias por el terreno!
Dom Hastings