Monday Mini-Golf # 5: <s> Jangan </s> JANGAN coba ini di rumah


24

Monday Mini-Golf: Serangkaian pertanyaan pendek , diposting (mudah-mudahan!) Setiap hari Senin.

Terkadang orang bosan dengan aturan hidup: "jangan lakukan ini", "kamu tidak bisa melakukan itu", "kami tidak akan membiarkan kamu melakukan ini". Kadang-kadang terlihat sangat membatasi! Tapi kadang-kadang, senang bersenang-senang, jadi mari kita menulis beberapa kode untuk mengubah aturan ini. Dan sementara kita berada di sana, mungkin juga memodifikasi hal-hal negatif lainnya. (Tentu saja, modifikasi ini tidak AKAN hanya bersifat sementara, jadi kami akan meninggalkan kata-kata aslinya juga.)

Tantangan

Tantangan Anda adalah menulis program atau fungsi yang membuat HTML <s>dicekik di</s> sekitar kata-kata yang membatasi ā€” yaitu, kata-kata yang diakhiri n'tatau diikuti oleh notā€” dan setelah masing-masing, memasukkan setara positifnya dalam SEMUA CAPS. Pada akhirnya, setelah spasi, jumlah penggantian yang dilakukan harus dimasukkan. Sebagai contoh:

Please don't jump into the pool.

menjadi

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

Untuk kata-kata yang diakhiri n'tatau diikuti oleh not(serta cannot), padanan positif adalah segalanya hingga yang disebutkan di atas not(tidak termasuk spasi). Inilah yang saya maksud:

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

Namun, ada beberapa pengecualian. Pastikan ini ditangani dengan benar.

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

Detail

  • Input tidak akan berisi spasi putih apa pun kecuali spasi normal (tidak ada tab, baris baru, dll.).
  • Masukan tidak akan pernah mengandung negatif ganda (misalnya we can't not do this).
  • Jika sebuah not muncul segera setelah tanda baca, atau sebagai bagian dari kata lain, biarkan saja.
  • Pastikan untuk menyimpan teks asli, termasuk huruf besar / kecil, di antara <s></s>tag.
  • Jika ingin, Anda dapat menggunakannya <strike></strike>sebagai pengganti <s></s>.

Kasus uji

Input:

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.

Output:

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

Mencetak gol

Ini adalah , jadi kode terpendek yang valid dalam byte menang. Tiebreaker pergi ke pengiriman yang mencapai jumlah byte terakhirnya terlebih dahulu. Pemenang tidak akan AKAN dipilih Senin depan, 26 Oktober. Semoga beruntung!


@FryAmTheEggman Ya, kecuali ada beberapa yang layak ditambahkan ke daftar. Saya lebih suka membuatnya singkat, tetapi apakah Anda punya saran?
ETHproduk

Tidak, hanya tidak ingin ini menderita dari sindrom tantangan "Anda mengubah spec sekarang itu menyebalkan": P
FryAmTheEggman

1
Can't we capitalize special cases?
Case

Apakah kita perlu berhati-hati dengan string seperti: Oh this? It's nothing...atauI wonder if we'll notice any words like this? ?
Dom Hastings

2
Satu <strike> tidak </strike> TIDAK hanya mengubah meme menjadi kontes!
Jan

Jawaban:


7

Pip , 138 140 byte

Yeesh, yang cannot/ knotperbedaan rumit.

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

Membaca garis dari stdin, output ke stdout. Regex:

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

cocok dengan satu kata (termasuk apostrof) diikuti oleh salah satu dari tiga hal:

  • not
  • n't
  • not tanpa spasi, asalkan paruh pertama kata itu can

Itu - operator pada regex membuat case-sensitive.

Kecocokan diganti dengan hasil dari fungsi berikut. (Catatan: dalam fungsi,a adalah seluruh pertandingan dan bgrup tangkap 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)

Setelah string yang dimodifikasi selesai, kami juga menampilkan spasi dan jumlah penggantian i.


3

GNU Sed, 321 bytes

(termasuk +1 untuk -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/ /

"Penggantian string - pekerjaan sed!" Saya pikir. Tetapi ini sangat sulit, dan saya terus menggunakan pengganti teks asli. Dan menghitung! Setidaknya tidak ada larangan unary dalam pertanyaan ...


2

Perl, 153 byte

150 skrip + 3 untuk -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
/

Jadi sementara saya cocok dengan semua test case sekarang tetapi regex itu sangat merugikan saya ... Saya akan memikirkannya!

Contoh output:

$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

Wow, ini ringkas! Salah satu isu: I'm nottidak bekerja dengan benar - itu harus I AMbukan I'M. Tapi saya pikir Anda hanya perlu perubahan I'muntuk i'mdalam kode untuk memperbaikinya.
DLosc

Oh - juga, coba di kalimat suka Tie a slipknot in the rope.atau Would you like Pinot Noir?. Mereka belum dalam uji kasus, tetapi niat OP adalah untuk mengecualikan kata yang diakhiri notkecuali cannot.
DLosc

@Dosc Kasus uji yang sangat baik! Saya tidak bisa memikirkan kata-kata lagi yang berakhir not! The I'madalah miss buruk, terima kasih untuk tempat!
Dom Hastings
Dengan menggunakan situs kami, Anda mengakui telah membaca dan memahami Kebijakan Cookie dan Kebijakan Privasi kami.
Licensed under cc by-sa 3.0 with attribution required.