'abc' and 'cba'

Your task is simple, write a single code snippet that when executed in one language outputs only the string 'abc' and when executed in another language outputs only the string 'cba'. The program should take no input.

This is a challenge.

• I don't think this is a dupe at all. The fact that the string is reversed is different enough from printing two different strings. I won't (vote to) reopen, though, as that would have immediate effect – Luis Mendo Aug 9 '17 at 20:00
• I voted to reopen this post, because IMO printing the reverse of a String and a String is much different than 2 different ones. No answer can trivially be modified to fit this challenge. My own answer uses a reverse symmetry techinque when compared to the answers there. I agree with @LuisMendo. – Mr. Xcoder Aug 9 '17 at 20:04
• Can you print ABC instead of abc – Oliver Ni Aug 10 '17 at 0:53
• I vote to reopen, some answers use the fact cba is abc backwards; link, link, link, link, link, link, link, link, link – Oliver Ni Aug 10 '17 at 2:37
• 'ABC' and 'CBA' is ok, leading/trailing whitespace/newlines ok but must be the same for both outputs – Chris_Rands Aug 10 '17 at 7:56

MATLAB / Octave, 41 bytes

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


In MATLAB randsample(2,2) gives a 2×1 vector, so size(...,2) is 1. Thus flip is applied along the first dimension, which is a singleton, so the original string 'abc' is displayed:

In Octave randsample(2,2) gives a 1×2 vector, so size(...,2) is 2. Thus flip is applied along the second dimension, that is, the string is flipped from left to right:

• I think this would be a few bytes shorter if you use the less interesting version variant. – Stewie Griffin Nov 20 '17 at 9:07
• @StewieGriffin Thanks. I think it's too late to change now. Maybe post it yourself? – Luis Mendo Nov 20 '17 at 10:09
• Nah, it's a boring version of this one... Also, I don't have MATLAB anymore so I won't be able to test it. – Stewie Griffin Nov 20 '17 at 10:15

05AB1E / Pyth, 5 bytes

_"abc


Try 05AB1E online!

Try Pyth online!

This also works in Pyke, outputting cba.

This also works in Recursiva, outputting cba.

Whitespace and Bash, 57 bytes



echo  cba



Try it online!

25 bytes

print(1/2and'cba'or'abc')

• -1 byte by using exit instead of print (although this applies to every answer as of now). – notjagan Aug 9 '17 at 18:42
• @notjagan. exit prints to stderr, doesn't it? Not sure if it would be a valid answer. – user72349 Aug 9 '17 at 18:47
• @ThePirateBay Outputing to STDERR is allowed as per meta consensus. – notjagan Aug 9 '17 at 18:51
• The challenge says code snippet and print though, it's not using default rules. – xnor Aug 9 '17 at 19:21

-1 byte if I make ==0 into >0 but that's already another answer

Python 2, 26 bytes

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


Try it online!

Python 3, 26 bytes

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


Try it online!

1/2 gives 0 (floordiv) in Python 2 and 0.5 (truediv) in Python 3. Thus, 1/2==0 gives 1 in Python 3 and 0 in Python 2 (actually, booleans, but those are just integers), so 'acbbca'[1::2] => 'cba' is given for Python 3 and 'acbbca'[0::2] => 'abc' is given for Python 2.

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

• Notepad isn't a programming language, but +1. – Jakob Aug 22 '17 at 20:36

Excel / Google Sheets, 41 28 27 24 Bytes

Anonymous worksheet formula that takes no input and outputs "ABC" to the calling cell in Excel and "CBA" to the calling cell in Google Sheets

=IfError(M("CBA"),"ABC")


In Google Sheets, M(...) is an alias for and autoformatted to T(...) (short for Text()). This call returns the text value of the passed variable, "CBA". "CBA" is not caught as an error, so "CBA" is returned by IfError(...,"ABC")

In Excel, there is no M(...) function, and M(...) is not an alias and therefore M("CBA") returns the formula not found error, #NAME?. This is caught by IfError(...,"ABC"), which in turn returns "ABC".

Previous Versions, 27, 28, 41 Bytes

See edits for explanations

=If(IsErr(A()),"ABC","CBA")

=If(IsErr(GT()),"ABC","CBA")

=IfError(If(Info("NUMFILE"),"ABC"),"CBA")

• Neat! ....You can save 2 bytes by using iserr instead of iferror and 1 byte by using "SYSTEM" instead of "NUMFILE": =IF(ISERR(INFO("SYSTEM")),"cba","abc") – Adam Aug 11 '17 at 19:23

CJam / 05AB1E, 6 bytes

"abc"R


Try it online:

How it works in CJam

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


How it works in 05AB1E

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

• Also works in Foo instead of CJam. – Makonede Feb 16 at 21:52

With apologies to @HyperNeutrino for stealing most of his answer (I don't have the reputation to comment yet)

Python 2, 25 bytes

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


Try it online!

Python 3, 25 bytes

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


Try it online!

• Umm it's actually <1 btw. – Erik the Outgolfer Aug 9 '17 at 18:42
• @EriktheOutgolfer No that's truthy for both languages – hyper-neutrino Aug 9 '17 at 18:43
• As @notjagan suggested, you can replace print with exit (which is allowed by our rules) and therefore save 1 byte. – user72349 Aug 9 '17 at 18:59
• @Zacharý Thanks, I didn't realize the problem had been updated (the original was only python 2 and pyhon 3) – reffu Aug 11 '17 at 12:28

JavaScript (NodeJS) and PHP, 46 bytes

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


Prints abc in JS and cba in PHP.

Try the JS online!

Try the PHP online! (note that TIO doesn't hide the HTML comments (<!--...-->)

• How is the <!-- interpreted in Node? – Esolanging Fruit Aug 9 '17 at 22:51
• @Challenger5 It's apparently interpreted as a one-line comment, exactly like // (source). Works that way in browser JS as well. – Justin Mariner Aug 9 '17 at 22:53
• That is weird... – Esolanging Fruit Aug 9 '17 at 23:06
• --> blah blah is valid comment in some JavaScript interpreter, you may just remove // – tsh Aug 10 '17 at 2:19
• @JustinMariner I had tested on Node v8.1.3. And the behavior defined in ES6 specification Annex B, which means all browsers that support ES6 should accept it as comment. – tsh Aug 10 '17 at 2:41

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"

• My first ever submission - hope I am doing it right.... – Adam Aug 11 '17 at 19:41
• Nice solution :) – Taylor Scott Aug 12 '17 at 14:16

Python / Befunge, 20 18 bytes

2 bytes saved thanks to @karhell

print("abc")# ,,,@


Try it online! for Python

Python sees print("abc") then a comment.

Try it online! for Befunge

Befunge, removing all nops and useless commands sees "abc",,,@ which puts a, b and c on the stack and then prints them (last in - first out).

• A bit late, but you can shave off two bytes by replacing >:#,_@ by ,,,@ – karhell Nov 20 '17 at 14:36
• Save one more with #,,<@ instead – Jo King Apr 22 '18 at 8:28

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 Aug 10 '17 at 8:18
• Or better still try:cmp;print('abc') – Chris_Rands Aug 10 '17 at 12:44
• Hmm... It doesn't seem to work... – totallyhuman Aug 11 '17 at 1:22
• That's not what I wrote, parantheses () still required around print – Chris_Rands Aug 11 '17 at 6:42

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

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

• I am strangely pleased by the way that cat( is totally ignored by Cubix. – Giuseppe Apr 23 '18 at 15:40

CJam and Gaia, 8 bytes

'c'b'a]$ Try it in CJam! Try it in Gaia! Explanation 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.

• I've added Gaia to the golfing language list; please let me know if I got any details wrong. – ETHproductions Aug 11 '17 at 14:13
• @ETHproductions Looks OK to me. – Business Cat Aug 11 '17 at 14:26

Java 8 & C, 95 bytes

//\
interface a{static void main(String[]s){System.out.print("abc"/*
main(){{puts("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");}}


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) 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 Aug 10 '17 at 0:43
• Or there's always the good old sizeof('x')>1?"abc":"cba" trick. – aschepler Aug 10 '17 at 1:24
• @Challenger5 Thanks for the comment – Ivan Botero Aug 10 '17 at 12:36
• @aschepler Thanks for the trick, i've made the changes 58 bytes :) – Ivan Botero Aug 10 '17 at 12:36
• sizeof's operand does not need parentheses, it's not a function. – hvd Aug 11 '17 at 18:33

Japt 2.0/JavaScript, 11 10 bytes

"abc"
//Uw


Japt 2.0 outputs cba

JavaScript outputs abc

Vyxal/Hexagony, 19 bytes

ka3wiṘ,Qλa;b@;<λ;c/


Try it Online! (Vyxal)

Try it Online! (Hexagony)

The program is split into two main parts, ka3wiṘ,Q and λa;b@;<λ;c/. The first half is just basic Vyxal to print "cba" (though I cannot use cba because it messes with Hexagony's layout). The second part worked out really nicely, because in order to print in Hexagony, a semicolon is required, but in Vyxal, an unmatched semicolon gives an error. So to fix this, I use lambdas in order to match them, and it just so happened to fit into an exact 3x3 hexagon. Luckily, in Hexagony almost any character that isn't special is taken as its codepoint, so the lambda character is quickly overwritten by anything else that comes after it.

The lambdas are placed so each one proceeds a semicolon, even if they aren't used in the hexagony code (as indicated by the blue trail).

Python 3, 26 bytes

print('abc'[::-(1/2>0)|1])


Try it online!

Python 2, 26 bytes

print('abc'[::-(1/2>0)|1])


Try it online!

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.

Python 2 and Foo, 16 bytes

print"abc"[::-1]


Python 2

print"abc"[::-1]


Try Python 2 online!

Explanation

print"abc"[::-1]

print             # print... (duh)
"abc"        # the string "abc"...
[::-1]  # backwards


Foo

"abc"


Try Foo online!

Explanation

"abc"

"abc"               print the string "abc"


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

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


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 ;

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)

• 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 Aug 10 '17 at 0:53
• shouldn't there be a ; for ><>? it wouldn't take any more bytes, just replace one of the spaces – Destructible Lemon Aug 10 '17 at 1:18
• How about "abc"&&ooo;? It makes Ly crash, but only after printing "abc". – Not a tree Aug 17 '17 at 13:05
• …or "abc"&&o!;o< for 1 extra byte, if you want to avoid crashing. – Not a tree Aug 17 '17 at 13:14

CJam and ><>, 12 bytes

"ooo;abc
"4>


What CJam sees:

"ooo;abc
"


String literal, which pushes the string ooo;abc (with a trailing newline) to the stack.

 4>


Slice off the first four characters of the string, leaving abc, which is output.

What ><> sees:

"


Begins a string literal.

 ooo;abc


Forms the contents of the string literal. The character codes of the characters in the string are pushed to the stack in reverse order (so c is on the top).

"


The IP wraps around, hitting the " a second time, which ends the string literal.

 ooo


Outputs the top three characters on the stack: cba

    ;


Terminates the program. Neither the abc nor any part of the second line is executed.

Solution with Error: 8 bytes

"abc"oo<


What CJam sees (Try it online!):

"abc"


Push a string literal to the stack.

    o


Output that string literal.

     o


Try to output again. The stack is empty, so the program crashes.

What ><> sees:

"abc"


Push three characters onto the stack in reverse order.

    o


Print one character: c

     o


Print another character: b

      <


Start moving backwards.

     o


Print the last character: a

    o


Try to print another character. Since the stack is empty, the program errors.

J/K (Kona), 30 bytes

NB. :0:"ABC"
{}[] /.echo'CBA'


Javascript / Ruby, 27 bytes

0?print('abc'):alert('cba')


A shorter version without prints for interpreters (14 bytes) :

0?'abc':'cba'


**Explanation**

NEW VERSION 0 is falsy for Javascript but truthy for Ruby, thanks to pxeger

OLD VERSION: '' is falsy for Javascript, but truthy for Ruby.

OLD VERSION: For Javascript, []+[] = "" as a string concatenation, since the + operand is only defined for numbers and strings, and the empty string is evaluated as false

Meanwhile, in Ruby, you can concatenate array using the + operand, and it is evaluated as true

(first participation in PCG ! :))

• Can't you use 0 instead of ''? – pxeger Jul 5 at 8:55
• You're right, thank you very much for your insight, I'll edit my answer :) – Tom Jul 6 at 9:15

Julia and Python 3, 23 bytes

print(["abc","cba"][1])


Try Julia online!

Try Python online!

Julia uses 1-based indexing while Python uses 0-based indexing

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. – hyper-neutrino Aug 10 '17 at 1:56
• I asked the OP and he hasn't responded. If this turns out to be invalid, I will remove it. – Oliver Ni Aug 10 '17 at 2:30
• @OliverNi Umm, if it's invalid you can just append a l` btw. – Erik the Outgolfer Aug 10 '17 at 11:02