您的任务很简单,编写一个代码段,当以一种语言执行时仅输出字符串,'abc'
而以另一种语言执行时仅输出字符串'cba'
。该程序不应该输入。
这是一个代码高尔夫挑战。
ABC
代替abc
'ABC'
和'CBA'
是好的,前/后空白/换行确定,但必须是两个输出相同的
您的任务很简单,编写一个代码段,当以一种语言执行时仅输出字符串,'abc'
而以另一种语言执行时仅输出字符串'cba'
。该程序不应该输入。
这是一个代码高尔夫挑战。
ABC
代替abc
'ABC'
和'CBA'
是好的,前/后空白/换行确定,但必须是两个输出相同的
Answers:
disp(flip('abc',size(randsample(2,2),2)))
在MATLAB中randsample(2,2)
给出2×1向量,因此size(...,2)
是1
。因此flip
沿第一个维度(即单例)应用,因此'abc'
显示了原始字符串:
在八度randsample(2,2)
给出了一个1×2矢量,所以size(...,2)
是2
。因此flip
沿第二维应用,即字符串从左向右翻转:
version
变体,它会短几个字节。
-1字节(如果==0
写入),>0
但这已经是另一个答案
print('acbbca'[1/2==0::2])
print('acbbca'[1/2==0::2])
1/2
0
在Python 2中给出(floordiv),在Python 3中给出(truediv)0.5
。因此,1/2==0
在Python 3中给出1,在Python 2中给出0(实际上是布尔值,但它们只是整数),因此'acbbca'[1::2] => 'cba'
对于Python 3 'acbbca'[0::2] => 'abc'
给出并且对于Python给出2。
匿名工作表公式,无需输入即可输出"ABC"
到Excel "CBA"
中的调用单元格和Google表格中的调用单元格
=IfError(M("CBA"),"ABC")
在Google表格中,M(...)
是的别名,并自动格式化为T(...)
(的缩写Text()
)。此调用返回传递的变量的文本值"CBA"
。"CBA"
没有被捕获为错误,因此"CBA"
由返回IfError(...,"ABC")
在Excel中,没有M(...)
函数,M(...)
也不是别名,因此M("CBA")
返回公式未找到错误#NAME?
。它被捕获IfError(...,"ABC")
,然后又返回"ABC"
。
参见编辑以获取解释
=If(IsErr(A()),"ABC","CBA")
=If(IsErr(GT()),"ABC","CBA")
=IfError(If(Info("NUMFILE"),"ABC"),"CBA")
iserr
代替来保存2个字节,iferror
并使用“ SYSTEM”代替“ NUMFILE” 来保存1个字节:=IF(ISERR(INFO("SYSTEM")),"cba","abc")
向@HyperNeutrino道歉,因为他窃取了他的大部分答案(我还不敢发表评论)
print('acbbca'[1/2>0::2])
print('acbbca'[1/2>0::2])
<1
顺便说一句。
try:exec("print'abc'")
except:print('cba')
Thought I'd try something different...
try:long;print('abc')
try:cmp;print('abc')
Inspired by @TaylorScott, who used a function that only exists in Excel, I found an even shorter function that only exists in Google Sheets. Conveniently, it is designed to return strings:
=iferror(join(,"cba"),"abc")
How it works
In Google Sheets, join([arg1], arg2, arg3,...argk)
will concatenate arg2 -> argk, optionally using the separator specified in arg1. In this case, it successfully returns "cba."
Excel has no join
function, so iferror
sees a problem and returns "abc"
'c'b'a]$
In both languages this defines a list of characters.
In CJam, $
is sort, so it becomes abc
.
In Gaia, $
joins the list into one string, giving cba
.
//\
interface a{static void main(String[]s){System.out.print("abc"/*
main(){{puts("cba"/**/);}}
Try it in Java 8 - resulting in "abc".
Try it in C - resulting in "cba".
Explanation:
//\
interface a{static void main(String[]s){System.out.print("abc"/*
main(){{puts("cba"/**/);}}
As you can see in the Java-highlighted code above, the first line is a comment due to //
, and the C-code is a comment due to /* ... */
, resulting in:
interface a{static void main(String[]s){System.out.print("abc");}}
//\
interface a{static void main(String[]s){System.out.print("abc"/*
main(){{puts("cba"/**/);}}
Not sure how to correctly enable C-highlighting, because lang-c
results in the same highlighting as Java.. But //\
will comment out the next line, which is the Java-code, resulting in:
main(){{puts("cba");}}
print('abc'[::int(1/2*4)-1])
In Python 2 int(1/2*4)-1
evaluates to -1
and so prints cba
. - TiO
In Python 3 it evaluates 1
so prints abc
. - TiO
#include<stdio.h>
main(){puts(sizeof('x')>1?"abc":"cba");}
78 bytes, thanks to challenger5.
58 bytes, thanks to aschepler.
56 bytes, thanks to hvd
#ifdef
s to make a single one. 2) You can remove the space in #include <stdio.h>
. 3) You can change printf("%s",
to puts(
. Try it online!
sizeof('x')>1?"abc":"cba"
trick.
sizeof
's operand does not need parentheses, it's not a function.
cat("abc")#u@o;o;o(;
For R, cat("abc")
then shameless abuse of comments.
For Cubix
c a
t (
" a b c " ) # u
@ o ; o ; o ( ;
. .
. .
"abc"
Pushs a, b ad c onto the stack)#
Increment the c, pushs number of element in stacku
U-turn to the right;(
Remove the count, Decrement the co;o;o@
Output cba and exitPushs the number on in stack
cat(
is totally ignored by Cubix.
print('abc'[::-(1/2>0)|1])
print('abc'[::-(1/2>0)|1])
25-byte version with exit
instead, which outputs to STDERR instead.
This is basically the same as print('abc'[::[1,-1][1/2>0]])
, just that it's golfed.
"abc"&&ov
; oo<
These languages are very similar, as Ly is based off ><>. However, Ly does not have 2D execution and interprets &
differently, which I took advantage of here.
Both languages will start by pushing abc
to the stack.
For ><>, the &
instruction moves values to and fro the register. Two in a row will push a value to the register and then take it straight back, essentially a NOP.
For Ly, &
is a modifier that makes an instruction perform its function on the entire stack.
o
means the same thing for both languages, but since it is modified by &
in Ly, it will print the whole stack, outputting abc
. In ><>, it will only output c
(as it is printed from the top down)
v
is a NOP in Ly, which skips it and goes straight to ;
, ending execution. ><> will instead treat it as a pointer, sending the IP downwards.
It then hits another arrow, sending the IP left. Here, it meets two o
signs, outputting b
and a
.
EDIT: Saved a byte (and fixed ><> crashing)
;
to the second line. This also has the benefit that the ><> IP doesn't wrap around and go through the second line again, which causes an error.
"abc"&&ooo;
? It makes Ly crash, but only after printing "abc".
"abc"&&o!;o<
for 1 extra byte, if you want to avoid crashing.
…CBAžR
Prints ABC
(OP said it was allowed) in 05AB1E and CBA
in 2sable, using the fact that 2sable was similar to 05AB1E but the žR
was added to 05AB1E after 2sable was abandoned.
l
btw.
This works because PHP interprets '0'
(same as the integer number 0
) as being falsy, while JavaScript assumes it is simply a non-empty string which is truthy.
'0'?alert('cba'):print(abc);
This is meant to run with -r
on PHP. In Javascript, just paste it in the console.
Thanks to @Justin Mariner for saving me 1 byte!
"0"
instead of +![]
: it becomes 0
(falsy) in PHP and is a string (truthy) in JS.
+![]?print(abc):alert('cba');
-> "0"?alert('cba'):print(abc);
using the -r
flag,
PHP sees die(abc)
and a comment, and since the compiler is forgiving, it outputs abc
as a string instead of an empty variable.
Brainf*ck only sees the instruction characters, and the rest is seen as comments.
die(abc);#--[----->+<]>---.-.-.
Try it online! (PHP)
Try it online! (Brainf*ck)
-r
if'a'=="a""abc"else"cba"end
In Octave, both 'a'
and "a"
represent the same string, therefore 'a'=="a"
is true. However, in Julia, 'a'
is a single character while "a"
is a one-character string. In Julia, "cba"
is the output.
Ungolfed version:
if 'a'=="a"
"abc"
else
"cba"
end
print'abc'.reverse
print
s cba
as we're calling .reverse
on the string.
print'abc'.reverse
print
s abc
concatenated with the result of reverse
which by default works on $_
which is empty and so makes no difference.