ITU CAPS LOCK DAY


29

22 OKTOBER ADALAH HARI KUNCI INTERNASIONAL CAPS ! LUAR BIASA, BEBERAPA JANGAN MENGAKUI KEMULIAAN KUNCI CAPS YANG KUAT. MEREKA KATAKAN ITU TAMPAKNYA "OBNOXIOUS" ATAU "LIKE SHOUTING" ATAU BEBERAPA NONSENSE. SEBAGAI ORANG UNTUK MENDAPATKAN KELUHAN INI YANG LUAR BIASA DAN KELUHAN INAN, HARAP MENULIS SAYA PROGRAM YANG MENGHIDUPKAN TEKS NORMAL KE DALAM "SENSIBLE" ATAU "WAJAR" TEKS UNTUK MEMBUAT KOMPLAIN STOP.

Deskripsi

Input dan output untuk solusi Anda adalah string yang hanya berisi karakter ASCII yang dapat dicetak.

String input akan berisi nol atau lebih caps lock yang berjalan . Sebuah caps lock menjalankan (atau CLR untuk pendek) didefinisikan sebagai berikut:

  • CLR tidak boleh mengandung huruf kecil ( a-z), kecuali sebagai karakter pertama dari sebuah kata .

    • Sebuah kata , untuk tujuan tantangan ini, adalah urutan non-spasi. Jadi, PPCG, correcthorsebatterystaple, dan jkl#@_>00()@#__f-023\f[semua dianggap kata s.
  • CLR juga harus mengandung setidaknya satu ruang; karenanya, paling tidak harus dua kata s.

  • Setiap kata dalam CLR harus mengandung setidaknya dua huruf ( A-Za-z).

    • Perhatikan bahwa ini merujuk pada CLR yang diambil dengan sendirinya, tanpa karakter di sekitarnya yang mungkin belum dimasukkan dalam CLR. Sebagai contoh, adalah tidak CLR karena string dengan sendirinya memiliki kata s dengan kurang dari dua huruf.foO BarO B

CLR harus diuraikan "dengan rakus" —yaitu, Anda harus selalu menemukan CLR terpanjang yang mungkin.

Setelah Anda mengidentifikasi semua CLR dalam string input, tukar casing semua huruf di dalam CLR dan output string yang dihasilkan.

Uji kasus

Baris pertama adalah input, dan yang kedua adalah output. Bagian tebal dari input adalah substring yang dianggap CLR.

CAPS LOCK IS THE BEST!
caps lock is the best!
I really LOVE pROGRAMMING pUZZLES AND cOde Golf!
I really love Programming Puzzles and Code Golf!
This is a challenge on PPCG. This is a test CASE. TEST
This is a challenge on PPCG. This is a test case. test
LorEM iPSUM DOLoR sIT amet, conSECTETur ADIPISciNG eLIT. MAECENAS iD orci
Lorem Ipsum doloR sIT amet, conSECTETur ADIPIScing Elit. maecenas Id orci
;'>}{/[]'"A*(389971(*(#$&B#@*(% c'>#{@D#$! :,>/;[e.[{$893F
;'>}{/[]'"a*(389971(*(#$&b#@*(% C'>#{@d#$! :,>/;[e.[{$893F
iT'S cAPS lOCK DAY!!! cELebraTE THis WONDERFUL key
It's Caps Lock day!!! Celebrate this WONDERFUL key
aBcDE fGHIj KLmNO pQrST (uVwXY) ZZ___Zz__Z
aBcde Fghij KLmno PqrST (uVwxy) zz___zz__Z
#aA# aA
#aA# aA

Aturan

  • Anda dapat berasumsi bahwa input tidak akan pernah mengandung dua atau lebih spasi dalam satu baris, dan input itu tidak akan pernah mengandung spasi awal atau akhir.

  • 20% bonus (kalikan panjang kode Anda dengan 0,8) jika seluruh kode Anda adalah CLR. ;) (kebanyakan hanya untuk bersenang-senang, karena pengajuan pemenang tidak akan memiliki bonus ini)

  • Ini adalah , jadi kode terpendek dalam byte menang.


16
Tolong, berhenti berteriak.
TheDoctor

4
Juga, untuk kasus uji # 3, bukankah PPCG yang dikapitalisasi juga menjadi lebih rendah? ( PPCG. Tmengandung spasi)
TheDoctor


2
@Dennis Saya membacanya dengan suara Morty (dari Rick dan Morty), dia berbicara dengan "Rick".
mbomb007

1
"poin bonus untuk kode Anda menjadi CLR" hanya membuat saya ingin melakukan ini di LOLCODE ...
cat

Jawaban:


4

CJam, 100 86 83 81 byte

Ml{_,),{1$<_S/(4$!>\1f>s+_eu=*S%_{'[,_el^:Af&s,2<},!*1>},_{W=/(AA26m>er}{;(}?\s}h

Coba biola ini dalam juru bahasa CJam atau verifikasi semua kasus uji sekaligus .

Algoritma

  1. Identifikasi CLR terpanjang yang mungkin dimulai dengan karakter pertama.

  2. Jika ada, tukar casingnya, cetak, dan lepaskan dari awal string.

    Lain, hapus satu karakter dari awal string, dan cetak itu tidak dimodifikasi.

  3. Jika ada lebih banyak karakter yang tersisa, kembali ke langkah 1.

Bagaimana itu bekerja

Ml         e# Push an empty string and a line from STDIN.
{          e# Do:
  _,       e#   Copy the string on the stack and compute its length (L).
  ),       e#   Push [0 ... L].
  {        e#   Filter; for each integer I in that array:
    1$<    e#     Copy the string and keep its first I characters.
    _S/    e#     Push a copy and split at spaces.
    (      e#     Shift out the first word.
    4$!    e#     Push the logical NOT of the fifth topmost item of the stack.
           e#     This pushes 1 for the empty string on the bottom, and 0
           e#     for non-empty strings and printable characters.
    >      e#     Remove that many characters from the beginning of the first word.
           e#     This will remove the first character iff the string on the
           e#     stack is the entire input. This is to account for the fact that
           e#     the first word is not preceded by a space.
    \1f>   e#     Remove the first character of all remaining words.
    s+     e#     Concatenate all of them.
    _eu=   e#     Convert a copy to uppercase and check for equality.
    *      e#     Repeat the I characters 1 or 0 times.
    S%_    e#     Split at runs of spaces, and push a copy.
    {      e#     Filter; for each non-empty word:
      '[,  e#       Push the string of all ASCII characters up to 'Z'.
      _el  e#       Push a copy and convert to lowercase.
      ^    e#       Perform symmetric difference, pushing all letters (both cases).
      :A   e#       Store the result in A.
      f&s  e#       Intersect A with each character of the word. Cast to string.
      s    e#       This removes all non-letters from the word.
      ,2<  e#       Count the letters, and compare the result to 2.
    },     e#     If there are less than 2 letters, keep the word.
    !      e#     Push the logical NOT of the result.
           e#     This pushes 1 iff all words contain enough letters.
    *      e#     Repeat the array of words that many times.
    1>     e#     Remove the first word.
  },       e#   Keep I if there are still words left.
  _{       e#   If at least one I was kept:
    W=     e#     Select the last (highest) one.
    /      e#     Split the string on the stack into chunks of that length.
    (      e#     Shift out the first chunk.
    AA26m> e#     Push "A...Za...z" and "a...zA...Z".
    er     e#     Perform transliteration to swap cases.
  }{       e#   Else:
    ;      e#     Discard the filtered array.
    (      e#     Shift out the first character of the string on the stack.
  }?       e#
  \s       e#   Swap the shifted out chunk/character with the rest of the string.
}h         e# If the remainder of the string is non-empty, repeat.

5
Cara kerjanya: memainkan 20 not E # pada piano.
kirbyfan64sos

Saya telah menambahkan beberapa detail lagi. : P
Dennis

2

Perl, 96 82 80 byte

-pe'$y=qr/[^a-z ]{2,}|\b\S[^a-z ]+/;s#$y( $y)+#join$,,map{uc eq$_?lc:uc}$&=~/./g#eg'

Lewati semua tes. Mengasumsikan input dari STDIN, dicetak ke STDOUT.

Bagaimana itu bekerja:

  • siapkan regex ( $y) yang cocok

    • setidaknya dua karakter huruf kecil, non-spasi putih ATAU
    • batas kata, diikuti oleh karakter non-spasi putih, diikuti oleh satu atau lebih non-huruf kecil, karakter non-spasi putih
  • cocokkan beberapa contoh string yang dipisahkan ruang yang cocok $y, gunakan s///untuk membalikkan huruf

Saya yakin ada ruang untuk perbaikan. Jika ada cara untuk menyingkirkan seluruh join-map-splitkesepakatan, masih ada peluang untuk mendapatkan bonus :)


1
Anda dapat menyimpan beberapa byte dengan menggunakan a-zalih-alih [:lower:]. Juga, -pebiasanya dihitung sebagai 1 byte, dan tanda kutip tunggal sebagai nol byte.
Dennis

@ Dennis: Terima kasih atas sarannya! Itu memungkinkan saya untuk menyederhanakan kode sedikit - turun ke 81 sesuai pedoman Anda tentang Perl one-liners
Zaid

Jawaban ini tidak valid, karena tidak lulus ujian terakhir (baru-baru ini ditambahkan oleh Dennis).
Gagang Pintu

2

Javascript, 193

decapslock =

a=>a.replace(/(^[a-z][^a-z ]+|[^a-z ]{2,})( [a-z][^a-z ]+| [^a-z ]{2,})+/g,b=>b.split` `.some(f=>f.split(/[a-z]/i).length<3)?b:b.split``.map(e=>e==(E=e.toUpperCase())?e.toLowerCase():E).join``)
<!-- Snippet UI -->
<input placeholder='sAMPLE tEXT' oninput="document.getElementsByTagName('p')[0].innerText=decapslock(this.value)" />
<p></p>

Penjelasan:

a=>a.replace(/* giant regex */,
  b=>
    b.split` `.some(
      f=>
        f.split(/[a-z]/i).length < 3   // check for 2+ letters
    )
      ? b                              // .some() immediately returns true if it's invalid
      : b.split``.map(                 // otherwise it's valid, so flip case
          e=>
            e == (E = e.toUpperCase()) // is it uppercase?
              ? e.toLowerCase()        // change it to LC
              : E                      // change it to UC, which was already done for
                                       // the case check
            ).join``
        )
(
^[a-z][^a-z ]+ // check for a CLR starting at the beginning with LC
|
[^a-z ]{2,}    // check for a CLR that begins in the middle of a word or starts at the
               // beginning with UC
               // in both cases, 2+ letters are required
)
(
 [a-z][^a-z ]+ // check for the next word of the CLR, starting with LC
|
 [^a-z ]{2,}   // check for the next word of the CLR, starting with UC
)+             // check for 1 or more next words

Jawaban ini tidak valid, karena tidak lulus ujian terakhir (baru-baru ini ditambahkan oleh Dennis).
Gagang Pintu

Argh, perbaikan menambahkan banyak byte ke ini. Tapi, sudah diperbaiki
DankMemes
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.