IT'S CAPS LOCK DAY


29

10月22日は国際キャップロックデーです!残念ながら、一部のプレイヤーは、Almighty Caps Lockの栄光を認識しません。彼らは「不快な」または「叫ぶような」またはなんらかのナンセンスだと言う。これらの明白な非言語的および不満の申し立てに準拠するために、人々を非難させるために、「わかりやすい」または「合理的な」テキストに通常のテキストを変換するプログラムを書いてください。

説明

ソリューションの入力と出力は、両方とも印刷可能なASCII文字のみを含む文字列になります。

入力文字列には、ゼロ以上のcaps lock runが含まれます。キャップは、実行ロック(略しまたはCLR)を以下のように定義されます。

  • CLRには、単語の最初の文字を除きa-z、小文字()を含めることはできません。

    • 言葉は、この課題の目的のために、非空間のシーケンスです。だから、PPCGcorrecthorsebatterystaple、とjkl#@_>00()@#__f-023\f[全く考慮されている単語の。
  • CLRには、少なくとも1つのスペースが含まれている必要があります。したがって、少なくとも2つの単語である必要があります。

  • CLRの各単語には、少なくとも2文字(A-Za-z)が含まれている必要があります。

    • これは、CLRに含まれていない可能性のある周囲の文字なしで、単独で取得されたCLRを指すことに注意してください。たとえば、文字列自体には2文字未満の単語 s があるため、はCLR ではありませんfoO BarO B

CLRは「貪欲に」解析する必要があります。つまり、可能な限り最も長いCLRを常に見つける必要があります。

入力文字列内のすべてのCLRを特定したら、CLR内のすべての文字の大文字と小文字を入れ替えて、結果の文字列を出力します。

テストケース

最初の行は入力で、2行目は出力です。入力の太字部分は、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

ルール

  • 入力に2つ以上のスペースが連続して含まれることはなく、先頭または末尾にスペースが含まれることはないと想定できます。

  • コード全体がCLRの場合、20%のボーナス(コード長に.8を掛けます)。;)(主に楽しみのためです。勝った提出物がこのボーナスを持つ可能性は低いからです)

  • これはであるため、バイト単位の最短コードが優先されます。


16
叫ぶのをやめてください。
TheDoctor

4
また、テストケース3では、大文字のPPCGも小文字になりませんか?(PPCG. Tスペースを含む)
TheDoctor


2
@Dennisモーティの声(リックとモーティから)で、彼が「リック」と話していることを読んだ。
mbomb007

1
ちょうど私がLOLCODEでこれを行うたくなる...「あなたのコードCLRであるため、ボーナスポイント」

回答:


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-
デニス

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'

すべてのテストに合格します。からの入力を想定しSTDIN、に出力STDOUTます。

使い方:

  • $y一致する正規表現()を設定します

    • 少なくとも2つの非小文字、非空白文字、または
    • 単語の境界と、それに続く非空白文字、それに続く1つ以上の非小文字、非空白文字
  • 大文字小文字を反転$yするs///ために使用する、スペースで区切られた文字列の複数のインスタンスに一致します。

改善の余地があると確信しています。join-map-split取引全体を取り除く方法があれば、ボーナスの資格を得るチャンスがまだあるかもしれません:)


1
a-z代わりにを使用すると、数バイトを節約できます[:lower:]。また、-pe通常は1バイトとしてカウントされ、単一引用符はゼロバイトとしてカウントされます。
デニス

@デニス:提案をありがとう!それは私には多少のコードを簡素化することができ- Perlのワンライナーについてのあなたのガイドラインどおり81にダウン
ザイド

最後のテストケースに合格しないため、この回答は無効です(最近デニスの好意により追加されました)。
ドアノブ

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

最後のテストケースに合格しないため、この回答は無効です(最近デニスの好意により追加されました)。
ドアノブ

ああ、修正プログラムはこれに大量のバイトを追加しました。しかし、それは修正されました
-DankMemes
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.