今天是锁定日


29

10月22日是国际大写锁定日!不幸的是,有些人无法识别全能大写锁的荣耀。他们说“讨厌”或“喜欢喊”或有些无意义。为了符合这些明显的逻辑和无理的投诉,请编写一个程序,将正常文本转换为“合理”或“合理”文本以使人们停止投诉。

描述

解决方案的输入和输出都将是仅包含可打印ASCII字符的字符串。

输入字符串将包含零个或多个大写锁定运行。甲大写锁定运行(或CLR的简称)被定义为如下:

  • CLR不得包含小写字母(a-z),除非单词的第一个字符。

    • 一个,对于这一挑战的目的,是无间隔的序列。所以PPCGcorrecthorsebatterystaplejkl#@_>00()@#__f-023\f[都被认为是秒。
  • CLR还必须至少包含一个空格;因此,它必须至少是两个单词 s。

  • CLR中的每个单词 s都必须包含至少两个字母(A-Za-z)。

    • 请注意,这是指CLR本身采取的措施,没有任何未包含在CLR中的周围字符。例如,它不是 CLR,因为字符串本身的单词 s少于两个字母。foO BarO B

应该“贪婪地”解析CLR,也就是说,您应该始终找到最长的CLR。

一旦确定了输入字符串中的所有CLR,请交换CLR中所有字母的大小写并输出结果字符串。

测试用例

输入第一行,输出第二行。输入的粗体部分是被视为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

规则

  • 您可能会假设输入将永远不会包含两个或多个空格,并且它将永远不会包含前导或尾随空格。

  • 如果您的整个代码是CLR,则可获得20%的奖金(代码长度乘以.8)。;)(主要是出于娱乐目的,因为获胜的提交者不太可能获得此奖金)

  • 这是,因此以字节为单位的最短代码获胜。


16
请别喊了
TheDoctor 2015年

4
另外,对于测试用例3,大写的PPCG也会小写吗?(PPCG. T包含一个空格)
TheDoctor 2015年


2
@丹尼斯我以莫蒂的声音(来自里克和莫蒂)读到,b / c他正在和“里克”说话。
mbomb007

1
只是让我想这样做在LOLCODE ......“为您的代码是一个CLR积分”

Answers:


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
丹尼斯2015年

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匹配的正则表达式()

    • 至少两个非小写的非空白字符,或者
    • 单词边界,后跟非空格字符,然后是一个或多个非小写,非空格字符
  • 匹配多个用空格分隔的匹配字符串的实例$y,用于s///反转大小写

我敢肯定还有改进的余地。如果有办法摆脱全部join-map-split交易,仍然有机会获得奖金:)


1
您可以使用a-z代替来节省一些字节[:lower:]。同样,-pe通常被计为1字节,单引号被计为零字节。
丹尼斯

@丹尼斯:谢谢你的建议!这使我可以稍微简化代码-根据您有关Perl
单线

该答案无效,因为它没有通过最后一个测试用例(最近由Dennis提供)。
门把手

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提供)。
门把手

糟糕,此修复程序为此增加了很多字节。但是,它是固定的
DankMemes,2015年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.