28
\$\begingroup\$

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.

\$\endgroup\$
  • 4
    \$\begingroup\$ 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 \$\endgroup\$ – Luis Mendo Aug 9 '17 at 20:00
  • 4
    \$\begingroup\$ 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. \$\endgroup\$ – Mr. Xcoder Aug 9 '17 at 20:04
  • 2
    \$\begingroup\$ Can you print ABC instead of abc \$\endgroup\$ – Oliver Ni Aug 10 '17 at 0:53
  • 5
    \$\begingroup\$ I vote to reopen, some answers use the fact cba is abc backwards; link, link, link, link, link, link, link, link, link \$\endgroup\$ – Oliver Ni Aug 10 '17 at 2:37
  • 2
    \$\begingroup\$ 'ABC' and 'CBA' is ok, leading/trailing whitespace/newlines ok but must be the same for both outputs \$\endgroup\$ – Chris_Rands Aug 10 '17 at 7:56

56 Answers 56

22
\$\begingroup\$

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.

\$\endgroup\$
21
\$\begingroup\$

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:

enter image description here

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:

enter image description here

\$\endgroup\$
  • \$\begingroup\$ I think this would be a few bytes shorter if you use the less interesting version variant. \$\endgroup\$ – Stewie Griffin Nov 20 '17 at 9:07
  • \$\begingroup\$ @StewieGriffin Thanks. I think it's too late to change now. Maybe post it yourself? \$\endgroup\$ – Luis Mendo Nov 20 '17 at 10:09
  • \$\begingroup\$ 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. \$\endgroup\$ – Stewie Griffin Nov 20 '17 at 10:15
15
\$\begingroup\$

25 bytes

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

Try it online! (Python 2)

Try it online! (Python 3)

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

Whitespace and Bash, 57 bytes

   		    	
 
 	
     	
	    
 	
     	
	   	
echo  cba



Try it online!

\$\endgroup\$
11
\$\begingroup\$

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

\$\endgroup\$
10
\$\begingroup\$

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")
\$\endgroup\$
  • 1
    \$\begingroup\$ 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") \$\endgroup\$ – Adam Aug 11 '17 at 19:23
8
\$\begingroup\$

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
\$\endgroup\$
8
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ Umm it's actually <1 btw. \$\endgroup\$ – Erik the Outgolfer Aug 9 '17 at 18:42
  • \$\begingroup\$ @EriktheOutgolfer No that's truthy for both languages \$\endgroup\$ – HyperNeutrino Aug 9 '17 at 18:43
  • \$\begingroup\$ As @notjagan suggested, you can replace print with exit (which is allowed by our rules) and therefore save 1 byte. \$\endgroup\$ – user72349 Aug 9 '17 at 18:59
  • 3
    \$\begingroup\$ You should add the second language to your answer (Python3 I assume) \$\endgroup\$ – Zacharý Aug 10 '17 at 20:51
  • \$\begingroup\$ @Zacharý Thanks, I didn't realize the problem had been updated (the original was only python 2 and pyhon 3) \$\endgroup\$ – reffu Aug 11 '17 at 12:28
8
\$\begingroup\$

Vim / Notepad.exe, 10 bytes

cbaabc<esc><backspace><backspace><backspace>
\$\endgroup\$
  • 2
    \$\begingroup\$ Notepad isn't a programming language, but +1. \$\endgroup\$ – Jakob Aug 22 '17 at 20:36
7
\$\begingroup\$

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 (<!--...-->)

\$\endgroup\$
  • 1
    \$\begingroup\$ How is the <!-- interpreted in Node? \$\endgroup\$ – Esolanging Fruit Aug 9 '17 at 22:51
  • \$\begingroup\$ @Challenger5 It's apparently interpreted as a one-line comment, exactly like // (source). Works that way in browser JS as well. \$\endgroup\$ – Justin Mariner Aug 9 '17 at 22:53
  • 6
    \$\begingroup\$ That is weird... \$\endgroup\$ – Esolanging Fruit Aug 9 '17 at 23:06
  • \$\begingroup\$ --> blah blah is valid comment in some JavaScript interpreter, you may just remove // \$\endgroup\$ – tsh Aug 10 '17 at 2:19
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – tsh Aug 10 '17 at 2:41
6
\$\begingroup\$

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

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

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

\$\endgroup\$
  • \$\begingroup\$ I like this one, not the shortest but quite a generic framework, can be shortened a bit by using try:long;print('abc') \$\endgroup\$ – Chris_Rands Aug 10 '17 at 8:18
  • \$\begingroup\$ Or better still try:cmp;print('abc') \$\endgroup\$ – Chris_Rands Aug 10 '17 at 12:44
  • \$\begingroup\$ Hmm... It doesn't seem to work... \$\endgroup\$ – totallyhuman Aug 11 '17 at 1:22
  • \$\begingroup\$ That's not what I wrote, parantheses () still required around print \$\endgroup\$ – Chris_Rands Aug 11 '17 at 6:42
5
\$\begingroup\$

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"

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

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.

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

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");}}
\$\endgroup\$
4
\$\begingroup\$

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

\$\endgroup\$
4
\$\begingroup\$

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!

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

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

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

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.

\$\endgroup\$
3
\$\begingroup\$

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"
\$\endgroup\$
3
\$\begingroup\$

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

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

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 ;

\$\endgroup\$
3
\$\begingroup\$

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)

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

Japt 2.0/JavaScript, 11 10 bytes

"abc"
//Uw

Japt 2.0 outputs cba

JavaScript outputs abc

\$\endgroup\$
3
\$\begingroup\$

J/K (Kona), 30 bytes

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

TIO - J & TIO - K kona

\$\endgroup\$
2
\$\begingroup\$

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)

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

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!

\$\endgroup\$
  • \$\begingroup\$ You could also use "0" instead of +![]: it becomes 0 (falsy) in PHP and is a string (truthy) in JS. \$\endgroup\$ – Justin Mariner Aug 10 '17 at 11:20
  • \$\begingroup\$ @JustinMariner You're right, but that's 1 byte longer. \$\endgroup\$ – Ismael Miguel Aug 10 '17 at 13:22
  • 1
    \$\begingroup\$ Isnt it one byte shorter? +![]?print(abc):alert('cba'); -> "0"?alert('cba'):print(abc); \$\endgroup\$ – Justin Mariner Aug 10 '17 at 13:46
  • \$\begingroup\$ @JustinMariner OH!!! That way!!! Yes, it is 1 byte shorter. Thank you! \$\endgroup\$ – Ismael Miguel Aug 10 '17 at 14:39
2
\$\begingroup\$

PHP / Brainf*ck, 31 bytes

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)

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.