首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >bROKEN cAPSLOCK kEY fIASCO

bROKEN cAPSLOCK kEY fIASCO
EN

Code Golf用户
提问于 2016-12-19 22:22:29
回答 6查看 3.8K关注 0票数 25

你的一些员工把车锁钥匙弄坏了,你太便宜了,无法更换。通过创建最短的程序来帮助他们纠正他们的工作!只需将给定字符串中的每个字符从大写转换为小写,反之亦然.但这有个转折!

圣诞节你也很兴奋!因此,您将留下一个小的"bug“,它不能纠正Christmas (大小写不敏感)序列中的字母。

输入

对于输入,您将使用一个字符串(或字节数组),它可能包含0x20和0x7e (- ~)之间的换行符和ascii。您不需要担心回车或字符串中的任何其他字符。

输出

输出应该只包含所提供的字符串,并交换大小写字符(当然还有圣诞错误!)它可以包含最多一个额外的尾随空格。

圣诞臭虫

让我们用一个例子来解释这一点:

代码语言:javascript
复制
Input: i CAN HARDLY WORK LIKE THIS please GET ME A NEW KEYBOARD FOR cHRISTMAS
Output: I Can HaRdly work lIke thiS PLEASE geT Me A new keyboard for ChriStmas

can包含"c“,这是圣诞节的第一个字母,所以没有改变。Christmas中的下一个字母是"h",它在hardly中(其中也包含"r"),因此不会更改,等等。Christmas本身只有一个字母不变,因为当代码到达那里时,它实际上是在查找"s",而不是"c“。

一旦找到序列,它应该在"c“处重新开始,并开始再次迭代Christmas。因此,ChristmasChristmas将保持不变。

测试用例

代码语言:javascript
复制
Input: Hello World!
Output: hELLO wORLD!

Input: I like pie :)
Output: i LIKE PIE :)

Input: hELP my KeYboarD
       iS BROKEN
Output: Help MY kEyBOARd
        Is broken

Input: cHRISTMAS IS COMING REALLY SOON!
Output: cHRISTMAS is Coming really soon!

Input: C is the first letter in cHRISTMAS
Output: C IS ThE FIrST LETTER iN ChriSTMAS

获胜者

这是密码-高尔夫,所以最短的答案获胜!

EN

回答 6

Code Golf用户

回答已采纳

发布于 2016-12-19 23:44:53

05AB1E,16字节

感谢Emigna保存了一个字节并修复了一个bug!

代码语言:javascript
复制
vyÐl'ŒÎ¾èQi¼ëš}?

解释:

代码语言:javascript
复制
vy                # For each character in the string...
  Ð               #   Triplicate that character
   l              #   Convert to lowercase
    'ŒÎ           #   Compressed version of "christmas"
       ¾          #   Push the counting variable (let's call this N)
        è         #   Get the nth character of "christmas", with modular indexing
         Qi   }   #   If equal...
           ¼      #      N += 1
            ë     #   Else...
             š    #      Swapcase
               ?  #   Print that character

使用CP-1252编码。在网上试试!

票数 9
EN

Code Golf用户

发布于 2016-12-22 21:02:53

C# 197字节

不会赢的,但是希望最小的C#实现能工作.

代码语言:javascript
复制
string C(string s){int i=0,j=0;var r="";for(;i<s.Length;){char c=s[i++],o=(char)32;if(c=="christmas"[j]|c=="CHRISTMAS"[j])j=j>7?0:j+1;else if(c>64&c<91)c+=o;else if(c>96&c<123)c-=o;r+=c;}return r;}

解释:

代码语言:javascript
复制
string C(string s)
{
    // define our two index ints
    // i for indexing across the input string
    // j for indexing across christmas
    int i = 0, j = 0;

    // r is our return string
    var r = "";

    // declare our loop
    // skip the initialisation and afterthought
    for (; i < s.Length;)
    {
        // get our current character c, and increment index i
        // initial our offset char o (difference between upper and lower case)
        char c = s[i++], o = (char)32;

        // check if c is the current character in our christmas bug
        if (c == "christmas"[j] | c == "CHRISTMAS"[j])
            // increment j (or reset to 0)
            j = j > 7 ? 0 : j + 1;

        // else if c is an upper case char
        else if (c > 64 & c < 91)
            // add our offset to make it lower case
            c += o;

        // else if c is lower case
        else if (c > 96 & c < 123)
            // subtract our offset to make it upper case
            c -= o;

        // append c to our return string r
        r += c;
    }

    return r;
}
票数 2
EN

Code Golf用户

发布于 2016-12-20 05:47:54

Ruby,63+1 = 64字节

使用-p标志。

代码语言:javascript
复制
i=0;gsub(/./){|c|c=~/#{"christmas"[i%9]}/i?(i+=1;c):c.swapcase}
票数 1
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/103761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档