Answers:
感谢@Kevin Lau节省了2个字节-不是Kenny
@CᴏɴᴏʀO'Bʀɪᴇɴ节省了2个字节
s=>s.replace(/[A-Z]/gi,l=>l+l[`to${l<"a"?"Low":"Upp"}erCase`]())
这使用了一个很hacky:
l[`to${l<"a"?"Low":"Upp"}erCase`]()
松散的是:
l[`to${
l < "a" ?
"Low" :
"Upp"
}erCase`]()
基本上l < "a"检查字母的代码点是否小于的代码点a(因此为大写字母)。如果是的话,它将变成to + Low + erCase哪个l['toLowerCase']()并使字符变为小写。`引号允许字符串格式化,因此从本质上讲,您可以想到:
`to${l < "a" ?"Low" : "Upp"}erCase`
as:"to" + (l<"a" ? "Low" : "Upp") + "erCase"生成要调用的函数(使字符串大写或小写)。我们将其放在方括号中[ ... ],这样我们就可以访问以字符串形式给出的属性。这将返回适当的函数,然后我们将其调用。
/[A-Z]/gi是较短的正则表达式:3
to${l<"a"?"Lower":"Upper"}Case到to${l<"a"?"Low":"Upp"}erCase
l[`to${l<"a"?"Low":"Upp"}erCase`]()我认为我们对邪恶有了新的定义。
f(char*s){for(;*s;s++)isalpha(putchar(*s))&&putchar(32^*s);}
使用事实'a' XOR 32 == 'A',等等。
多亏了FryAmTheEggman,节省了三个字节。
&&使用*,不是吗?
&&短路行为是如何工作的,我很确定这两种方法都不起作用。
f(char*s){isalpha(putchar(*s))&&putchar(32^*s);*s&&f(1+s);}递归的?
f(char*s){*s&&f(1+s,isalpha(putchar(*s))&&putchar(32^*s));}递归的?
l_el_eu.+.|
l e# Read input.
_el e# Duplicate, convert to lower case.
_eu e# Duplicate, convert to upper case.
.+ e# Concatenate the two characters in matching positions from those two
e# strings. E.g. "ab!" "AB!" would give ["aA" "bB" "!!"].
e# For each character from the original string and the corresponding
.| e# string from this list, take the set union (which eliminates duplicates
e# and keeps the order the values appear in from left to right, so that
e# the original case of each letter comes first).
sm{+dr2
测试套件。
sm{+dr2 input: Q
sm{+dr2dQ implicit arguments
Q input
m for each character as d:
r2d swapcase
+d prepend d
{ deduplicate
s join as string
for i in input():print(end=i+i.swapcase()[:i.isalpha()])
完整程序。会尝试更多的高尔夫运动。
(s)->s.chars.map((i)->{if String.letters has i.lower{if i<"a"{i+i.lower}else{i+i.upper}}else{i}}).join()
第一个真正的切达干酪答案!!!这比我想象的要少得多。
与非竞争版本1.0.0-beta.9一起使用。
如您所知,我不是将切达干酪设计成高尔夫球状的:/
取消高尔夫:
(str) -> str.chars.map(
(i) -> {
if String.letters has i {
if i < "a" { // Check char code, meaning it's upper case if true
i+i.lower
}
else {
i+i.upper
}
} else {
i
}
}
).join()
用法:
var doThing = <code here>;
doThing("input...");
更新: 16年7月14日,我完成了三元运算,将其减少到84个字节
(s)->s.chars.map((i)->String.letters has i.lower?i<"a"?i+i.lower:i+i.upper:i).join()
[A-Za-z]->i`[A-Z]
_。我将使用选项卡,以便我可以一次测试所有测试用例。
将字符串作为输入传递f(),并将输出写入STDOUT。该字符串未修改。
f(char*v){for(;*v;++v)putchar(*v),isalpha(*v)?putchar(*v-32+64*!islower(*v)):0;}
[:;]<@~."1@,.tolower,.toupper
[:;]<@~."1@,.tolower,.toupper Input: s
toupper Convert s to all uppercase
tolower Convert s to all lowercase
,. Join them as columns in a 2d array
] Identity function, get s
,. Prepend s as a column to the 2d array
~."1@ Take the unique chars on each row
<@ Box them
[:; Unbox the list of boxes and join their contents and return
import Data.Char
g n|isLower n=toUpper n|1<2=toLower n
(>>= \x->x:[g x|isAlpha x])
isLower比具有的构造要短elem5个字节。
>>=是concatMap(或concat.map)参数已翻转:f n = n >>= (\x->if isAlpha x then[x,r x]else[x])。你可以去pointfree并省略函数名和替换的定义f有(>>= \x->if isAlpha x then[x,r x]else[x])。
otherwise您可以使用任何计算结果为的表达式代替True,例如1<2。您可以将if .. then .. else列表理解替换为\x->[x]++[g x|isAlpha x]。哦,有一个错误:第二toUpper中g必须是toLower。
[x]++是x:。
-n标志)s/[a-z]/$&.(ord$&<97?lc$&:uc$&)/ige
(-p需要标签)
(由于@Dom Hasting,-2个字节)
简短说明:
ord返回一个char的数值。ord(any lower case) >= 97和ord(any upper case) <= 90)。
运行:
perl -pe 's/[a-z]/$&.(ord$&<97?lc$&:uc$&)/ige'
/i否则您的正则表达式将在字母之间匹配多个代码点。
带有-p标志,运行
gsub(/(?<=(.))/){$1.swapcase!}
利用这样的事实,swapcase!它将返回nil除ASCII字母之外的任何东西,当从gsub块中返回时,它将转换为空字符串。@Jordan通过捕获后面的字符来节省了一个字节。
//然后再使用$`[-1]很聪明。
gsub(/(?<=(.))/){$1.swapcase!}。不过,基本概念相同,请随时使用。
.swapcase!。(我的意思是,删除该!。)
paste0(x<-unlist(strsplit(readline(),"")),gsub("[^A-Za-z]","",chartr("a-zA-Z","A-Za-z",x)),collapse="")
readline()可以使用,但是会花费一个字节
scan将使用用引号引起来的输入(通常是其他语言的命令行参数的情况)
码:
vyyš«Ù?
说明:
v # For each in input.
yyš # Push y and y swapcased.
«Ù # Concatentate and uniquify.
? # Print without a newline.
使用CP-1252编码。在线尝试!
tYov"@uv!
说明
% Implicitly grab input as string
t % Duplicate the input
Yo % Swap case of all characters
v % Vertically concatenate the original and swap-cased versions
" % For each column (letter in the original)
@u % Compute the unique values (without sorting)
v! % Vertically concatenate with the existing output and transpose
% Implicit end of for loop and implicit display
-p flag)s/[a-z]/$&.$&^$"/ige
$" instead of ' ', but I haven't tested.
Thanks to @recursive for a byte saved!
┤§ÆP♦■
Run and debug it at staxlang.xyz! (link is to unpacked version)
c:~\{um
c:~\{um
c Copy the top element of the stack (the input, in this case).
:~ Switch case of each letter in the copy.
\ Zip. This produces an array of two-character strings.
{ m Map a block over this array of two-character strings.
u Get all unique elements.
Implicit concatenate and print.
u instead of :g. It will get all the unique elements in an array, which is exactly what you want in this case. Other than that, this looks well golfed.
123. You may need to change the format for all inputs (i.e. quote them). The link is also broken. You need to replace m=11 with m=2. There is a PPCG post generating button on staxlang.xyz so you may want to use that one.
lambda s:''.join((x,x+x.swapcase())[x.isalpha()]for x in s)
Edited to fix repeating non-alphabetic characters
not s is (list comprehension) okay, Julia...
This code assumes access through a web server (Apache, for example), using the default configuration.
You can pass the string by sending the key S by any means (POST, GET, COOKIE, SESSION...).
<?for($i=0;$c=$S[$i++];)echo$c,ctype_alpha($c)?$c^' ':'';
s=>string.Concat(s.Select(c=>c+(char.IsLetter(c)?(char)(c^32)+"":"")));
C# lambda where the input and the output are string. Try it online.
11 bytes thanks to @Lynn trick.
(defun f(s)(let((b""))(dotimes(i(length s))(if(lower-case-p(elt s i))(progn #1=(setf b(concatenate 'string b(string #2=(elt s i))))(setf b(concatenate 'string b(string(char-upcase #2#)))))(progn #1#(setf b(concatenate 'string b(string(char-downcase #2#)))))))b))
ungolfed:
(defun f (s)
(let ((b ""))
(dotimes (i (length s))
(if (lower-case-p (elt s i))
(progn
#1=(setf b (concatenate 'string b (string #2=(elt s i))))
(setf b (concatenate 'string b (string (char-upcase #2#)))))
(progn
#1#
(setf b (concatenate 'string b (string (char-downcase #2#)))))))
b))
Usage:
CL-USER 1 > (f "abc")
"aAbBcC"
CL-USER 2 > (f "bad")
"bBaAdD"