你的一些员工把车锁钥匙弄坏了,你太便宜了,无法更换。通过创建最短的程序来帮助他们纠正他们的工作!只需将给定字符串中的每个字符从大写转换为小写,反之亦然.但这有个转折!
圣诞节你也很兴奋!因此,您将留下一个小的"bug“,它不能纠正Christmas (大小写不敏感)序列中的字母。
对于输入,您将使用一个字符串(或字节数组),它可能包含0x20和0x7e (- ~)之间的换行符和ascii。您不需要担心回车或字符串中的任何其他字符。
输出应该只包含所提供的字符串,并交换大小写字符(当然还有圣诞错误!)它可以包含最多一个额外的尾随空格。
让我们用一个例子来解释这一点:
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 ChriStmascan包含"c“,这是圣诞节的第一个字母,所以没有改变。Christmas中的下一个字母是"h",它在hardly中(其中也包含"r"),因此不会更改,等等。Christmas本身只有一个字母不变,因为当代码到达那里时,它实际上是在查找"s",而不是"c“。
一旦找到序列,它应该在"c“处重新开始,并开始再次迭代Christmas。因此,ChristmasChristmas将保持不变。
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这是密码-高尔夫,所以最短的答案获胜!
发布于 2016-12-19 23:44:53
感谢Emigna保存了一个字节并修复了一个bug!
vyÐl'ŒÎ¾èQi¼ëš}?解释:
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编码。在网上试试!
发布于 2016-12-22 21:02:53
不会赢的,但是希望最小的C#实现能工作.
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;}解释:
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;
}发布于 2016-12-20 05:47:54
使用-p标志。
i=0;gsub(/./){|c|c=~/#{"christmas"[i%9]}/i?(i+=1;c):c.swapcase}https://codegolf.stackexchange.com/questions/103761
复制相似问题