35
\$\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\$
9
  • 5
    \$\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\$ Aug 10 '17 at 7:56

64 Answers 64

23
\$\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\$
3
  • \$\begingroup\$ I think this would be a few bytes shorter if you use the less interesting version variant. \$\endgroup\$ 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\$ Nov 20 '17 at 10:15
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\$
0
16
\$\begingroup\$

Whitespace and Bash, 57 bytes

   		    	
 
 	
     	
	    
 	
     	
	   	
echo  cba



Try it online!

\$\endgroup\$
1
15
\$\begingroup\$

25 bytes

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

Try it online! (Python 2)

Try it online! (Python 3)

\$\endgroup\$
4
  • \$\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
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\$
0
11
\$\begingroup\$

Vim / Notepad.exe, 10 bytes

cbaabc<esc><backspace><backspace><backspace>
\$\endgroup\$
1
  • 3
    \$\begingroup\$ Notepad isn't a programming language, but +1. \$\endgroup\$
    – Jakob
    Aug 22 '17 at 20:36
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
  • 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\$
1
  • 1
    \$\begingroup\$ Also works in Foo instead of CJam. \$\endgroup\$
    – Makonede
    Feb 16 at 21:52
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\$
5
  • \$\begingroup\$ Umm it's actually <1 btw. \$\endgroup\$ Aug 9 '17 at 18:42
  • \$\begingroup\$ @EriktheOutgolfer No that's truthy for both languages \$\endgroup\$
    – hyper-neutrino
    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\$
    – Adalynn
    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
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\$
7
  • 1
    \$\begingroup\$ How is the <!-- interpreted in Node? \$\endgroup\$ 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\$ Aug 9 '17 at 22:53
  • 7
    \$\begingroup\$ That is weird... \$\endgroup\$ 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\$

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\$
2
  • 1
    \$\begingroup\$ My first ever submission - hope I am doing it right.... \$\endgroup\$
    – Adam
    Aug 11 '17 at 19:41
  • \$\begingroup\$ Nice solution :) \$\endgroup\$ Aug 12 '17 at 14:16
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\$
2
  • \$\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\$
4
  • \$\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\$ Aug 10 '17 at 8:18
  • \$\begingroup\$ Or better still try:cmp;print('abc') \$\endgroup\$ Aug 10 '17 at 12:44
  • \$\begingroup\$ Hmm... It doesn't seem to work... \$\endgroup\$ Aug 11 '17 at 1:22
  • \$\begingroup\$ That's not what I wrote, parantheses () still required around print \$\endgroup\$ Aug 11 '17 at 6:42
5
\$\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\$
1
5
\$\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\$
1
  • 2
    \$\begingroup\$ I am strangely pleased by the way that cat( is totally ignored by Cubix. \$\endgroup\$
    – Giuseppe
    Apr 23 '18 at 15:40
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\$
2
  • \$\begingroup\$ I've added Gaia to the golfing language list; please let me know if I got any details wrong. \$\endgroup\$ Aug 11 '17 at 14:13
  • \$\begingroup\$ @ETHproductions Looks OK to me. \$\endgroup\$ 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\$

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\$
6
  • 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\$ 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\$ Aug 10 '17 at 12:36
  • \$\begingroup\$ @aschepler Thanks for the trick, i've made the changes 58 bytes :) \$\endgroup\$ 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\$

Japt 2.0/JavaScript, 11 10 bytes

"abc"
//Uw

Japt 2.0 outputs cba

JavaScript outputs abc

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

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

\$\endgroup\$
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\$
0
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\$
4
  • 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\$ 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\$ 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\$

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.

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

J/K (Kona), 30 bytes

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

TIO - J & TIO - K kona

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

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 ! :))

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

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

\$\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\$
3
  • \$\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\$
    – hyper-neutrino
    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\$ Aug 10 '17 at 11:02

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.