มันล็อควัน


29

ตุลาคม 22 เป็นวันหยุดสากลล็อค ! โดยไม่ได้รับอนุญาตบางคนไม่ได้รับการยกย่องความรุ่งเรืองของ CAPS LOCK อันยิ่งใหญ่ พวกเขาบอกว่ามันเป็น "OBNOXIOUS" หรือ "LIKE SHOUTING" หรือบางครั้งไม่รู้สึก เพื่อให้สอดคล้องกับคำศัพท์ที่ผิดปกติและผิดปกติเหล่านี้โปรดเขียนโปรแกรมที่ทำให้ข้อความปกติกลายเป็น "SENSIBLE" หรือ "REASONABLE" TEXT เพื่อทำให้คนหยุดการร้องเรียนโดยสมบูรณ์

ลักษณะ

อินพุตและเอาต์พุตสำหรับโซลูชันของคุณจะเป็นสตริงที่มีอักขระ ASCII ที่พิมพ์ได้เท่านั้น

สายป้อนจะมีศูนย์หรือมากกว่าcaps lock วิ่ง Caps Lock ทำงาน (หรือ CLR สั้น) ถูกกำหนดให้เป็นต่อไปนี้:

  • CLR ที่ต้องไม่มีอักษรตัวพิมพ์เล็ก ( a-z) ยกเว้นเป็นตัวอักษรตัวแรกของคำว่า

    • คำสำหรับวัตถุประสงค์ของความท้าทายนี้เป็นลำดับที่ไม่ใช่ช่องว่าง-A ดังนั้นPPCG, correcthorsebatterystapleและjkl#@_>00()@#__f-023\f[ได้รับการพิจารณาทุกคำ s
  • CLR ต้องมีอย่างน้อยหนึ่งช่องว่าง ดังนั้นจะต้องมีอย่างน้อยสองคำ s

  • คำแต่ละคำใน CLR ต้องมีตัวอักษรอย่างน้อยสองตัว ( A-Za-z)

    • โปรดทราบว่าสิ่งนี้หมายถึง CLR ที่ดำเนินการโดยตัวมันเองโดยไม่มีตัวอักษรล้อมรอบที่อาจไม่รวมอยู่ใน CLR ยกตัวอย่างเช่นเป็นไม่ CLR เพราะสตริงด้วยตัวเองมีคำ s ที่มีน้อยกว่าสองตัวอักษรfoO BarO B

ควรแยกวิเคราะห์ CLR "อย่างตะกละตะกลาม" นั่นคือคุณควรหา CLR ที่ยาวที่สุดเท่าที่จะทำได้

เมื่อคุณระบุ CLR ทั้งหมดในสตริงอินพุตให้สลับกรณีของตัวอักษรทั้งหมดภายใน CLRs และส่งออกสตริงผลลัพธ์

กรณีทดสอบ

บรรทัดแรกคืออินพุตและบรรทัดที่สองคือเอาต์พุต ส่วนที่เป็นตัวหนาของอินพุตเป็นสตริงย่อยที่ถูกพิจารณาว่าเป็น 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

กฎระเบียบ

  • คุณอาจสมมติว่าอินพุตจะไม่มีช่องว่างสองช่องขึ้นไปในแถวและจะไม่มีช่องว่างนำหน้าหรือต่อท้าย

  • โบนัส 20% (คูณความยาวโค้ดของคุณด้วย. 8) หากรหัสทั้งหมดของคุณคือ CLR ;) (ส่วนใหญ่เป็นเพียงเพื่อความสนุกสนานเนื่องจากไม่น่าที่การส่งที่ชนะจะได้รับโบนัสนี้)

  • นี่คือดังนั้นโค้ดที่สั้นที่สุดในหน่วยไบต์ชนะ


16
กรุณาหยุดตะโกน
TheDoctor

4
นอกจากนี้สำหรับกรณีทดสอบ # 3 จะไม่มีการจ่ายเงินต่อหนึ่งคลิกของ PPCG ที่ลดลงด้วยหรือไม่ ( PPCG. Tมีช่องว่าง)
TheDoctor


2
@ เดนนิสฉันอ่านว่าด้วยเสียงของ Morty (จาก Rick และ Morty), b / c เขากำลังพูดกับ "Rick"
mbomb007

1
"คะแนนโบนัสสำหรับรหัสของคุณเป็น CLR" เพียงแค่ทำให้ฉันต้องการทำสิ่งนี้ใน LOLCODE ...
cat

คำตอบ:


4

CJam, 100 86 83 81 ไบต์

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

ลองซอนี้ในล่าม CJam หรือตรวจสอบกรณีทดสอบทั้งหมดในครั้งเดียว

ขั้นตอนวิธี

  1. ระบุ CLR ที่ยาวที่สุดที่เป็นไปได้ที่เริ่มต้นด้วยอักขระตัวแรก

  2. หากมีอยู่ให้สลับกรณีพิมพ์และลบออกจากจุดเริ่มต้นของสตริง

    อื่นลบอักขระเดี่ยวจากจุดเริ่มต้นของสตริงและพิมพ์โดยไม่แก้ไข

  3. หากมีอักขระเหลืออยู่ให้กลับไปที่ขั้นตอนที่ 1

มันทำงานอย่างไร

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
มันทำงานอย่างไร: เล่นโน้ต 20 E # บนเปียโน
kirbyfan64sos

ฉันได้เพิ่มรายละเอียดเพิ่มเติมอีกสองสามข้อ : P
Dennis

2

Perl, 96 82 80 ไบต์

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

ผ่านการทดสอบทั้งหมด ถือว่าข้อมูลจากพิมพ์ไปSTDINSTDOUT

มันทำงานอย่างไร:

  • ตั้งค่า regex ( $y) ที่ตรงกับ

    • อย่างน้อยสองตัวอักษรที่ไม่ใช่ตัวพิมพ์เล็กและไม่ใช่ช่องว่างหรือ
    • ขอบเขตของคำตามด้วยอักขระที่ไม่ใช่ช่องว่างตามด้วยอักขระอย่างน้อยหนึ่งตัวไม่ใช่ตัวเว้นวรรค
  • จับคู่สตริงที่คั่นด้วยช่องว่างหลายอินสแตนซ์ที่ตรงกับ$yใช้s///เพื่อสลับกรณี

ฉันแน่ใจว่ามีห้องพักสำหรับการปรับปรุง หากมีวิธีกำจัดjoin-map-splitข้อตกลงทั้งหมดอาจยังมีโอกาสได้รับโบนัส :)


1
คุณสามารถบันทึกไม่กี่ไบต์โดยใช้แทนa-z [:lower:]นอกจากนี้-peมักจะนับเป็น 1 ไบต์และเครื่องหมายคำพูดเดี่ยวเป็นศูนย์ไบต์
เดนนิส

@Dennis: ขอบคุณสำหรับคำแนะนำ! ที่ทำให้ฉันง่ายรหัสค่อนข้าง - ลดลงถึง 81 ตามแนวทางของคุณเกี่ยวกับ Perl one-liners
Zaid

คำตอบนี้ไม่ถูกต้องเนื่องจากไม่ผ่านกรณีทดสอบครั้งล่าสุด (เพิ่งได้รับความอนุเคราะห์จาก Dennis)
Doorknob

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>

คำอธิบาย:

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

คำตอบนี้ไม่ถูกต้องเนื่องจากไม่ผ่านกรณีทดสอบครั้งล่าสุด (เพิ่งได้รับความอนุเคราะห์จาก Dennis)
Doorknob

อย่างไรก็ตาม, การแก้ไขเพิ่มตันจำนวนนี้ แต่มันได้รับการแก้ไข
DankMemes
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.