'abc'和'cba'


28

您的任务很简单,编写一个代码段,当以一种语言执行时仅输出字符串,'abc'而以另一种语言执行时仅输出字符串'cba'。该程序不应该输入。

这是一个挑战。


4
我认为这根本不是骗子。字符串反转的事实与打印两个不同的字符串有很大不同。不过,我不会(投票给)重新开放,因为那会立即生效
Luis Mendo

4
我投票决定重新开放这篇文章,因为IMO打印String和String的反面与2个不同的字符串有很大不同。没有任何答案可以轻易修改以适应这一挑战。与那里的答案相比,我自己的答案使用反对称技术。我同意@LuisMendo。
Xcoder先生17年

2
您可以ABC代替abc
Oliver Ni

5
我投票重新开放,一些答案使用的事实cbaabc倒退的;链接链接链接链接链接链接链接链接链接
Oliver Ni

2
'ABC''CBA'是好的,前/后空白/换行确定,但必须是两个输出相同的
Chris_Rands

Answers:



21

MATLAB /八度,41字节

disp(flip('abc',size(randsample(2,2),2)))

在MATLAB中randsample(2,2)给出2×1向量,因此size(...,2)1。因此flip沿第一个维度(即单例)应用,因此'abc'显示了原始字符串:

enter image description here

在八度randsample(2,2)给出了一个1×2矢量,所以size(...,2)2。因此flip沿第二维应用,即字符串从左向右翻转:

enter image description here


我认为如果使用不太有趣的version变体,它会短几个字节。
Stewie Griffin

@StewieGriffin谢谢。我认为现在进行更改为时已晚。也许自己张贴?
Luis Mendo

不,这是一个无聊的版本...而且,我不再有MATLAB,所以我将无法对其进行测试。
Stewie Griffin



10

-1字节(如果==0写入),>0但这已经是另一个答案

Python 2,26个字节

print('acbbca'[1/2==0::2])

在线尝试!


Python 3,26个字节

print('acbbca'[1/2==0::2])

在线尝试!

1/20在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。


10

Excel / Google表格,41 28 27 24字节

匿名工作表公式,无需输入即可输出"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"


以前的版本,27,28,41字节

参见编辑以获取解释

=If(IsErr(A()),"ABC","CBA")
=If(IsErr(GT()),"ABC","CBA")
=IfError(If(Info("NUMFILE"),"ABC"),"CBA")

1
整齐!....您可以使用iserr代替来保存2个字节,iferror并使用“ SYSTEM”代替“ NUMFILE” 来保存1个字节:=IF(ISERR(INFO("SYSTEM")),"cba","abc")
亚当(Adam)

8

CJam / 05AB1E,6个字节

"abc"R

在线尝试:

它如何在CJam中工作

"abc"    Push this string
R        Push variable R, predefined to the empty string
         Implicitly display stack

它如何在05AB1E中工作

"abc"    Push this string
R        Reverse
         Implicitly display top of the stack

8

向@HyperNeutrino道歉,因为他窃取了他的大部分答案(我还不敢发表评论)

Python 2,25个字节

print('acbbca'[1/2>0::2])

在线尝试!

Python 3,25个字节

print('acbbca'[1/2>0::2])

在线尝试!


嗯,实际上是<1顺便说一句。
暴民埃里克(Erik the Outgolfer)'17年

@EriktheOutgolfer不,两种语言都是如此
HyperNeutrino

如@notjagan所建议,您可以替换printexit(我们的规则允许),因此节省了1个字节。

3
你应该第二语言添加到您的答案(Python3我假设)
扎卡里

@Zacharý谢谢,我没有意识到问题已更新(原来只有python 2和pyhon 3)
reffu

8

Vim / Notepad.exe,10个字节

cbaabc<esc><backspace><backspace><backspace>

2
记事本不是编程语言,而是+1。
雅各布

7

JavaScript(NodeJS)和PHP,46字节

<!--
strrev=console.log//--><?=
strrev("abc");

abc用JS和cbaPHP 打印。

在线尝试JS!

在线尝试PHP!(请注意,TIO不会隐藏HTML注释(<!--... -->


1
如何<!--在Node中解释?
硕果累累

@ Challenger5显然是单行注释,就像//source)。在浏览器JS中也是如此。
贾斯汀·马里纳

6

--> blah blah是某些JavaScript解释器中的有效注释,您可以删除//
tsh

1
我在Node v8.1.3上测试过@JustinMariner ES6规范附件B中定义的行为,这意味着所有支持ES6的浏览器都应接受它作为注释。
tsh

6

Python / Befunge,20 18字节

@karhell节省了2个字节

print("abc")# ,,,@

在线尝试!对于Python

print("abc")然后,Python看到一条注释。

在线尝试!对于Befunge

Befunge,删除所有空指令和无用的指令看到"abc",,,@这使abc在堆栈上,然后打印它们(后进-先出)。


有点晚了,但是您可以替换>:#,_@成两个字节来,,,@
删除

Save one more with #,,<@ instead
Jo King

5

Python 2 and Python 3, 42 bytes

try:exec("print'abc'")
except:print('cba')

Try it online! (Python 2)

Try it online! (Python 3)

Thought I'd try something different...


I like this one, not the shortest but quite a generic framework, can be shortened a bit by using try:long;print('abc')
Chris_Rands

Or better still try:cmp;print('abc')
Chris_Rands


That's not what I wrote, parantheses () still required around print
Chris_Rands

5

Excel/Google Sheets, 28 bytes

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"


1
My first ever submission - hope I am doing it right....
Adam

Nice solution :)
Taylor Scott


4

Java 8 & C, 95 bytes

//\
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");}}

4

Python 2 / Python 3, 28 bytes

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


2
Welcome to Programming Puzzles and Code Golf
Евгений Новиков

4

C and C++, 115, 78, 58, 56 bytes

#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

Try it - C++!

Try it - C!


1
1) You can collapse the two #ifdefs 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!
Esolanging Fruit

2
Or there's always the good old sizeof('x')>1?"abc":"cba" trick.
aschepler

@Challenger5 Thanks for the comment
Ivan Botero

@aschepler Thanks for the trick, i've made the changes 58 bytes :)
Ivan Botero

1
sizeof's operand does not need parentheses, it's not a function.
hvd

4

R/Cubix, 20 bytes

cat("abc")#u@o;o;o(;

R - Try it online!

Cubix - Try it online!

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 stack
  • u U-turn to the right
  • ;( Remove the count, Decrement the c
  • o;o;o@ Output cba and exit

Pushs the number on in stack


2
I am strangely pleased by the way that cat( is totally ignored by Cubix.
Giuseppe



3

C (gcc) C++ (g++), 59 bytes

#include<stdio.h>
main(){puts("abc\0cba"+(sizeof(' ')&4));}

3

Fission / ><> , 11 bytes

!R"abc"ooo;

Try Fission Online

In Fission, a particle starts at R and prints abc.

Try ><> Online

In ><>, the IP starts at the top-left. ! skips the next instruction, and "abc" pushes [a,b,c] on the stack. ooo then pops and prints three times, giving cba.

Both programs end at ;


3

Ly / ><>, 20 19 bytes

"abc"&&ov
;     oo<

Try it with ><>!

Try it with Ly!

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)


1
You can save a byte by moving the ; 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.
Esolanging Fruit

shouldn't there be a ; for ><>? it wouldn't take any more bytes, just replace one of the spaces
Destructible Lemon

How about "abc"&&ooo;? It makes Ly crash, but only after printing "abc".
Not a tree

…or "abc"&&o!;o< for 1 extra byte, if you want to avoid crashing.
Not a tree



2

05AB1E and 2sable, 6 bytes

…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.

Try it online! (05AB1E)

Try it online! (2sable)


The specification states that it must be "abc" or "cba". By my word, I'd say that this is invalid, but I can ask OP.
HyperNeutrino

I asked the OP and he hasn't responded. If this turns out to be invalid, I will remove it.
Oliver Ni

@OliverNi Umm, if it's invalid you can just append a l btw.
Erik the Outgolfer

2

PHP + JavaScript, 29 28 bytes

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!


You could also use "0" instead of +![]: it becomes 0 (falsy) in PHP and is a string (truthy) in JS.
Justin Mariner

@JustinMariner You're right, but that's 1 byte longer.
Ismael Miguel

1
Isnt it one byte shorter? +![]?print(abc):alert('cba'); -> "0"?alert('cba'):print(abc);
Justin Mariner

@JustinMariner OH!!! That way!!! Yes, it is 1 byte shorter. Thank you!
Ismael Miguel


2

Julia and Octave/Matlab, 27 bytes

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

2

Perl / Ruby, 18 bytes

Ruby

print'abc'.reverse

prints cba as we're calling .reverse on the string.

Try it online!

Perl

print'abc'.reverse

prints abc concatenated with the result of reverse which by default works on $_ which is empty and so makes no difference.

Try it online!

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.