'abc' and 'cba'

28

1

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.

Chris_Rands

Posted 2017-08-09T18:24:55.367

Reputation: 887

4I 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 – 2017-08-09T20:00:30.600

4I 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 – 2017-08-09T20:04:08.997

Reopening now, as it has two votes in addition to mine – Luis Mendo – 2017-08-09T20:26:00.867

Is a leading newline allowed? – Zacharý – 2017-08-09T23:50:06.810

2Can you print ABC instead of abc – Oliver Ni – 2017-08-10T00:53:35.723

5

I vote to reopen, some answers use the fact cba is abc backwards; link, link, link, link, link, link, link, link, link

– Oliver Ni – 2017-08-10T02:37:33.107

2'ABC' and 'CBA' is ok, leading/trailing whitespace/newlines ok but must be the same for both outputs – Chris_Rands – 2017-08-10T07:56:14.297

1@OliverNi Another reason it might not be considered a dupe is that the other question says "Different versions of the same language don't count", whereas that is allowed and indeed encouraged here – Chris_Rands – 2017-08-10T08:04:31.753

Answers

22

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

Luis Mendo

Posted 2017-08-09T18:24:55.367

Reputation: 87 464

I think this would be a few bytes shorter if you use the less interesting version variant. – Stewie Griffin – 2017-11-20T09:07:59.140

@StewieGriffin Thanks. I think it's too late to change now. Maybe post it yourself? – Luis Mendo – 2017-11-20T10:09:08.843

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 – 2017-11-20T10:15:06.567

15

25 bytes

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

Try it online! (Python 2)

Try it online! (Python 3)

Erik the Outgolfer

Posted 2017-08-09T18:24:55.367

Reputation: 38 134

-1 byte by using exit instead of print (although this applies to every answer as of now). – notjagan – 2017-08-09T18:42:58.567

@notjagan. exit prints to stderr, doesn't it? Not sure if it would be a valid answer. – None – 2017-08-09T18:47:38.377

@ThePirateBay Outputing to STDERR is allowed as per meta consensus.

– notjagan – 2017-08-09T18:51:11.480

The challenge says code snippet and print though, it's not using default rules. – xnor – 2017-08-09T19:21:31.587

15

Whitespace and Bash, 57 bytes

   		    	
 
 	
     	
	    
 	
     	
	   	
echo  cba



Try it online!

aschepler

Posted 2017-08-09T18:24:55.367

Reputation: 717

1

You can golf 3 bytes by removing the three trailing new-lines. Relevant Whitespace tip.

– Kevin Cruijssen – 2017-08-17T13:47:48.973

10

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

HyperNeutrino

Posted 2017-08-09T18:24:55.367

Reputation: 26 575

10

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

Taylor Scott

Posted 2017-08-09T18:24:55.367

Reputation: 6 709

1Neat! ....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 – 2017-08-11T19:23:13.337

8

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!

reffu

Posted 2017-08-09T18:24:55.367

Reputation: 1 361

Umm it's actually <1 btw. – Erik the Outgolfer – 2017-08-09T18:42:34.810

@EriktheOutgolfer No that's truthy for both languages – HyperNeutrino – 2017-08-09T18:43:51.143

As @notjagan suggested, you can replace print with exit (which is allowed by our rules) and therefore save 1 byte.

– None – 2017-08-09T18:59:22.800

3You should add the second language to your answer (Python3 I assume) – Zacharý – 2017-08-10T20:51:24.997

@Zacharý Thanks, I didn't realize the problem had been updated (the original was only python 2 and pyhon 3) – reffu – 2017-08-11T12:28:29.623

8

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

Luis Mendo

Posted 2017-08-09T18:24:55.367

Reputation: 87 464

8

Vim / Notepad.exe, 10 bytes

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

m-chrzan

Posted 2017-08-09T18:24:55.367

Reputation: 1 390

2Notepad isn't a programming language, but +1. – Jakob – 2017-08-22T20:36:05.393

7

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

Justin Mariner

Posted 2017-08-09T18:24:55.367

Reputation: 4 746

1How is the <!-- interpreted in Node? – Esolanging Fruit – 2017-08-09T22:51:46.333

@Challenger5 It's apparently interpreted as a one-line comment, exactly like // (source). Works that way in browser JS as well.

– Justin Mariner – 2017-08-09T22:53:59.097

6That is weird... – Esolanging Fruit – 2017-08-09T23:06:26.040

--> blah blah is valid comment in some JavaScript interpreter, you may just remove // – tsh – 2017-08-10T02:19:46.883

@tsh That doesn't seem to work in any of the TIO JS interpreters; where does that act as a comment? – Justin Mariner – 2017-08-10T02:24:19.850

1@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 – 2017-08-10T02:41:19.853

Might as well link the spec. And that's definitely weird when used alone. Fortunately it only works on a line where's there's no statement (only whitespace and inline comments)

– Aaron – 2017-08-11T12:19:40.537

6

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

Uriel

Posted 2017-08-09T18:24:55.367

Reputation: 11 708

A bit late, but you can shave off two bytes by replacing >:#,_@ by ,,,@ – karhell – 2017-11-20T14:36:42.260

Save one more with #,,<@ instead – Jo King – 2018-04-22T08:28:57.927

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

totallyhuman

Posted 2017-08-09T18:24:55.367

Reputation: 15 378

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 – 2017-08-10T08:18:51.650

Or better still try:cmp;print('abc') – Chris_Rands – 2017-08-10T12:44:25.453

Hmm... It doesn't seem to work... – totallyhuman – 2017-08-11T01:22:19.430

That's not what I wrote, parantheses () still required around print – Chris_Rands – 2017-08-11T06:42:46.180

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"

Adam

Posted 2017-08-09T18:24:55.367

Reputation: 151

1My first ever submission - hope I am doing it right.... – Adam – 2017-08-11T19:41:12.350

Nice solution :) – Taylor Scott – 2017-08-12T14:16:58.337

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!

Ivan Botero

Posted 2017-08-09T18:24:55.367

Reputation: 301

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 – 2017-08-10T00:43:53.683

    2Or there's always the good old sizeof('x')>1?"abc":"cba" trick. – aschepler – 2017-08-10T01:24:22.523

    @Challenger5 Thanks for the comment – Ivan Botero – 2017-08-10T12:36:20.540

    @aschepler Thanks for the trick, i've made the changes 58 bytes :) – Ivan Botero – 2017-08-10T12:36:42.307

    1sizeof's operand does not need parentheses, it's not a function. – hvd – 2017-08-11T18:33:46.393

    @hvd Thanks! You're right! – Ivan Botero – 2017-08-11T19:53:16.510

    4

    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.

    Business Cat

    Posted 2017-08-09T18:24:55.367

    Reputation: 8 927

    I've added Gaia to the golfing language list; please let me know if I got any details wrong.

    – ETHproductions – 2017-08-11T14:13:32.850

    @ETHproductions Looks OK to me. – Business Cat – 2017-08-11T14:26:09.143

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

    Kevin Cruijssen

    Posted 2017-08-09T18:24:55.367

    Reputation: 67 575

    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

    user52452

    Posted 2017-08-09T18:24:55.367

    Reputation:

    2Welcome to Programming Puzzles and Code Golf – Евгений Новиков – 2017-08-11T10:48:04.640

    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

    MickyT

    Posted 2017-08-09T18:24:55.367

    Reputation: 11 735

    2I am strangely pleased by the way that cat( is totally ignored by Cubix. – Giuseppe – 2018-04-23T15:40:11.703

    3

    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.

    Mr. Xcoder

    Posted 2017-08-09T18:24:55.367

    Reputation: 39 774

    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)

    LyricLy

    Posted 2017-08-09T18:24:55.367

    Reputation: 3 313

    1You 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 – 2017-08-10T00:53:32.783

    shouldn't there be a ; for ><>? it wouldn't take any more bytes, just replace one of the spaces – Destructible Lemon – 2017-08-10T01:18:19.517

    How about "abc"&&ooo;? It makes Ly crash, but only after printing "abc". – Not a tree – 2017-08-17T13:05:32.527

    …or "abc"&&o!;o< for 1 extra byte, if you want to avoid crashing. – Not a tree – 2017-08-17T13:14:42.840

    3

    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"
    

    totallyhuman

    Posted 2017-08-09T18:24:55.367

    Reputation: 15 378

    3

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

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

    tsh

    Posted 2017-08-09T18:24:55.367

    Reputation: 13 072

    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 ;

    KSmarts

    Posted 2017-08-09T18:24:55.367

    Reputation: 1 830

    3

    Japt 2.0/JavaScript, 11 10 bytes

    "abc"
    //Uw
    

    Japt 2.0 outputs cba

    JavaScript outputs abc

    Oliver

    Posted 2017-08-09T18:24:55.367

    Reputation: 7 160

    3

    J/K (Kona), 30 bytes

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

    TIO - J & TIO - K kona

    jayprich

    Posted 2017-08-09T18:24:55.367

    Reputation: 391

    2

    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.

    Esolanging Fruit

    Posted 2017-08-09T18:24:55.367

    Reputation: 13 542

    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)

    Oliver Ni

    Posted 2017-08-09T18:24:55.367

    Reputation: 9 650

    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 – 2017-08-10T01:56:14.623

    I asked the OP and he hasn't responded. If this turns out to be invalid, I will remove it. – Oliver Ni – 2017-08-10T02:30:17.660

    @OliverNi Umm, if it's invalid you can just append a l btw. – Erik the Outgolfer – 2017-08-10T11:02:50.973

    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!

    Ismael Miguel

    Posted 2017-08-09T18:24:55.367

    Reputation: 6 797

    You could also use "0" instead of +![]: it becomes 0 (falsy) in PHP and is a string (truthy) in JS. – Justin Mariner – 2017-08-10T11:20:49.290

    @JustinMariner You're right, but that's 1 byte longer. – Ismael Miguel – 2017-08-10T13:22:04.213

    1Isnt it one byte shorter? +![]?print(abc):alert('cba'); -> "0"?alert('cba'):print(abc); – Justin Mariner – 2017-08-10T13:46:39.243

    @JustinMariner OH!!! That way!!! Yes, it is 1 byte shorter. Thank you! – Ismael Miguel – 2017-08-10T14:39:02.987

    2

    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)

    Stan Strum

    Posted 2017-08-09T18:24:55.367

    Reputation: 436

    FYI, you don't have to count -r

    – Business Cat – 2017-08-10T19:31:14.727

    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
    

    Sven Hohenstein

    Posted 2017-08-09T18:24:55.367

    Reputation: 2 464

    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!

    Dom Hastings

    Posted 2017-08-09T18:24:55.367

    Reputation: 16 415

    2

    Python and Ruby, 30 bytes

    print(['abc','cba'][(0 or 1)])
    

    Ruby: the logical or short-circuits on 0, which is a truthy value.

    Python: 0 is false, and the logical or yields 1.

    G B

    Posted 2017-08-09T18:24:55.367

    Reputation: 11 099

    1

    Python 2, 27 bytes

    print(['abc','cba'][1/2>0])
    

    Try it online!

    Python 3, 27 bytes

    print(['abc','cba'][1/2>0])
    

    Try it online!

    HyperNeutrino

    Posted 2017-08-09T18:24:55.367

    Reputation: 26 575

    3What is the purpose of posting another answer which is even more verbose than your first one? – None – 2017-08-09T18:41:37.187

    1@ThePirateBay It is a completely different approach – Mr. Xcoder – 2017-08-09T18:42:08.210

    @ThePirateBay I think it's different enough. Besides, it's such a trivial task that (at least in my opinion), half of these answers are pretty much the same but reworded. – HyperNeutrino – 2017-08-09T18:42:18.487

    1

    Python 2 and Python 3, 31 bytes

    print(round(.5)and"abc"or"cba")
    

    Uses the and/or trick from Erik the Outgolfer's answer, as well as the fact that Python 3 uses banker's rounding, while Python 2 does not.

    Try it - Python 2!

    Try it - Python 3!

    Stephen

    Posted 2017-08-09T18:24:55.367

    Reputation: 12 293

    Oh, thanks for having that as a note, I need to modify the MY interpreter now. And nice golf too! – Zacharý – 2017-08-09T23:55:07.747

    @Zacharý is there any good reason to still support Python 2? :P – Stephen – 2017-08-10T00:21:18.700

    Yes. Because people still use Python2 (including me, since I have nltk and sympy on Py2) – Zacharý – 2017-08-10T01:03:42.890

    1

    Python 2, 28 bytes

    print("abc"[::1/2and-1or 1])
    

    Try it online!

    Python 3, 28 bytes

    print("abc"[::1/2and-1or 1])
    

    Try it online!

    Mr. Xcoder

    Posted 2017-08-09T18:24:55.367

    Reputation: 39 774

    1

    JavaScript (ES8 & ES6), 24 bytes

    _=>"".padEnd?"abc":"cba"
    

    Shaggy

    Posted 2017-08-09T18:24:55.367

    Reputation: 24 623

    What about ES5 vs ES6 test [].keys – tsh – 2017-08-10T02:46:35.137

    Or even ES5 vs ES3 test [].map... – tsh – 2017-08-10T02:52:02.960

    @tsh; yeah, there's loads of options here. I went with this one 'cause it allowed me to use an arrow function, rather than an alert. – Shaggy – 2017-08-11T08:28:10.117

    1

    05AB1E and MY, 22 bytes

    Try it online! (05AB1E)

    Hex String in Python:

    '"abc"?\x8e\x01\x06\x89(\x02\x06\x89(\x80\x03\x06\x89(\x80&'
    

    Rendered in 05AB1E's codepage:

    "abc"?Žʒε‰(αε‰(€βε‰(€&
    

    Rendered in MY's unimplemented codepage:

    _RrH_⌈≡16ǵ'26ǵ'+36ǵ'+←
    

    How?

    Here is what 05AB1E sees:

    "abc"?Žʒε‰(αε‰(€βε‰(€&
    "abc"                   -  The string "abc"
         ?                  -  Print (no newline)
          Ž                 -  Break on empty stack
           ʒε‰(αε‰(€βε‰(€&  - Unread junk
    

    What MY sees (I added a feature which causes errors to produce 0, in addition to fixing the r command.):

    _RrH_⌈≡16ǵ'26ǵ'+36ǵ'+←
    _                       Negate (0)
     R                      Convert to binary ([0])
      r                     Convert from binary (0)
       H                    Convert to hex ("0")
        _                   Negate (errors, so 0)
         ⌈                  Ceiling (0)
          ≡                 Match (0 0=>1)
           16ǵ'             'a'
               26ǵ'+        Prepend 'b'
                    36ǵ'+   Prepend 'c'
                         ←  Output without a newline
    

    I could have used , but I don't want to divide by 0 to result in 0.

    (Use the file polyglot.py)

    Zacharý

    Posted 2017-08-09T18:24:55.367

    Reputation: 5 710

    If you changed MY after the question was posted to make this answer work, you should probably mark the answer as "non-competing" in the header. – aschepler – 2017-08-11T22:27:15.983

    1There's a meta post saying that that isn't necessary, IIRC. – Zacharý – 2017-08-11T22:29:26.037

    1

    Braingolf and Lean Mean Bean Machine, 35 bytes

     O
    ""
    #a
    !$
    ""
    #b
    !$_
    ""
    #c
    & ,&
    @U
    

    Braingolf: Try it online!
    No LMBM online interpreter available

    Outputs abc in LMBM, and cba in Braingolf.

    Explanation

    LMBM is a 2d language, which allows us to selectively execute the code we want.
    Meanwhile Braingolf is a 1d language, meaning it starts at the top left, and goes across each line, executing code. It terminates upon reaching the end of the code.

    In LMBM, a marble is spawned at every uppercase O when the program starts. Each tick it drops 1 character and executes the character. LMBM terminates once all marbles are destroyed.

    A double quote " in LMBM sets the marble to char mode, meaning the next character to execute is interpreted as a 1 char string literal, and the marble's value is set to the codepoint of the character.

    However " begins and terminates a string in Braingolf, automatically pushing the contents to the stack. Thankfully an empty string does nothing.

    $ in LMBM prints the marble's value as a char, however this is the silent modifier in Braingolf, so we have to make sure to consume that modifier before we try to print the stack, that's what the ! and _ are for (_ pops and prints the top of the stack, with the $ modifier it doesn't print, and with the ! modifier it doesn't pop, so !$_ consumes both modifiers without actually doing anything)

    In Braingolf # is the char literal identifier, causing the codepoint of the next char to be pushed to the stack.

    After passing through all of the characters (and in LMBM's case printing the first 2) we hit the last 2 lines.
    The 2nd to last line does nothing in LMBM, as it simply drops through the space, a no-op.

    In Braingolf however, the ampersand & is the greedy modifier. Modifiers in Braingolf are held until an operator consumes them, so the first ampersand is held through the space until it hits the comma , Braingolf's flip operator. The greedy modifier is consumed here to cause Braingolf to flip the entire stack, rather than the top 2 items. Then another greedy modifier is hit.
    This one is held through the newline to the at @ sign, this prints the entire stack as ASCII.

    Meanwhile LMBM simply drops through the space into the U, which prints the marble's value as ASCII and destroys the marble.
    The U also has a function in Braingolf, it pops the top of the stack and pushes range(0...n), but the stack is empty because we just popped and printed everything, so this does nothing.

    Skidsdev

    Posted 2017-08-09T18:24:55.367

    Reputation: 9 656

    1

    C / Common Lisp, 91 88 40 36 bytes

    //(princ"abc")
    ;main(){puts("cba");}
    

    Try it online – C

    Try it online – Common Lisp

    Both “normal” languages with very different syntax and semantics!

    Explanation

    For Common Lisp:

    // is a special variable used in the Common Lisp REPL to give the result of the value preceding the previous one, initially equal to NIL. Then we prints abc. The following line starts with a semicolon, which is used to start one-line comments.

    For C:

    the first line is a comment, and the other is the program. Note that the semicolon before the definition of the main function is syntactically correct.

    -1 byte thanks to @Zacharý

    Renzo

    Posted 2017-08-09T18:24:55.367

    Reputation: 2 260

    Would it be possible to remove the space after #include, since it's read as ;include? (Or at least from your explanation) – Zacharý – 2017-08-11T11:23:23.553

    @Zacharý, yes, thanks! – Renzo – 2017-08-11T19:07:51.713

    1

    Javascript / Ruby, 28 bytes

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

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

    ''?'abc':'cba'
    


    Explanation

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

    Tom

    Posted 2017-08-09T18:24:55.367

    Reputation: 11

    1

    C/><>, 58 bytes

    #include <stdio.h>//^
    int main(){puts(/*/*/"abc"//ooo;
    );}
    

    Sasha

    Posted 2017-08-09T18:24:55.367

    Reputation: 431

    1

    V/Vim, 6 bytes

    iabcæ
    

    Try it online!

    V: cba

    Vim: abc

    V and Vim are backwards compatible, but V has many more commands mapped. In V, æ means <M-f>lip where it's meaningless in Vim.

    nmjcman101

    Posted 2017-08-09T18:24:55.367

    Reputation: 3 274

    1

    JavaScript / Pascal, 40 bytes

    JavaScript

    Outputs "abc".

    {alert`abc`/*}begin write('cba')end.{*/}

    Pascal

    Outputs "cba".

    {alert`abc`/*}begin write('cba')end.{*/}
    

    Try it online!


    Pascal conveniently uses { } for block comments and, in JavaScript, { } are just scopes, which affect nothing in this context.

    Matheus Avellar

    Posted 2017-08-09T18:24:55.367

    Reputation: 273

    1

    C and x86 DOS binary, 58 bytes

    /* VX4!PZSXNN4s(D (D"5s@SCYAAA@!6cba*/main(){puts("abc");}
    

    gastropner

    Posted 2017-08-09T18:24:55.367

    Reputation: 3 264

    1

    Pyth / Canvas, 14... bytes?

    <4"abcjjj±" \┐
    

    Try it in Pyth!
    Try it in Canvas!


    Pyth explanation:

    <4                # All but the last 4 characters of:
      "abcjjj±"    # String literal
                   \┐ # Char literal, printing suppressed
    
    Python 3 translation:
    print("abcjjj±"[:-4])
    "┐"
    

    Canvas explanation:

    Code               | Explanation                         | Stack
    -------------------+-------------------------------------+----------------
    <4"abc             | Push "<4\"abc" to the stack         | "<4\"abc"
          jjj        |Remove first three characters of ToS | "abc"
               ±       | Reverse ToS                         | "cba"
                 " \   | Push "\" \\" to stack               | "cba", "\" \\"
                     ┐ | Remove ToS                          | "cba"
                       | Print ToS (implicit)                |
    

    hakr14

    Posted 2017-08-09T18:24:55.367

    Reputation: 1 295

    2I don't think this is valid unless you can provide a hexdump. – Esolanging Fruit – 2018-04-22T07:31:33.913

    1

    brainfuck / Jelly, 24 bytes

    -[+[>+<<]>+]>+.+.+.
    “CBA
    

    is encoded as 0xFE in Jelly's codepage. Jelly's codepage is backwards compatible with printable ASCII, so all other characters are encoded the same.

    To brainfuck

    Try it online!

    -[+[>+<<]>+]> sets a cell to 64. +.+.+. increments and outputs three times, printing ABC.

    To Jelly

    Try it online!

    The first line is parsed as valid Jelly, but is ignored.

    The second line is a string literal with content CBA. The last line of the file is the main link, so this is printed.

    Esolanging Fruit

    Posted 2017-08-09T18:24:55.367

    Reputation: 13 542

    1

    Swift / JavaScript, 34 bytes

    /*/*/alert`cba`// */*/print("abc")
    

    Try it Online! (Swift)

    Testing snippet (JavaScript):

    /*/*/alert`cba`// */*/print("abc")

    This works by abusing nested block comments in Swift. Swift interprets the code as /*/* Comment */*/print("abc") while JavaScript interprets the code as /*Comment*/alert`cba`//Comment

    Herman L

    Posted 2017-08-09T18:24:55.367

    Reputation: 3 611

    1

    Japt/Japt 2.0, 14 bytes

    ;AÊ?"abc":"cba
    

    Japt outputs cba

    Japt 2.0 outputs abc

    Oliver

    Posted 2017-08-09T18:24:55.367

    Reputation: 7 160

    0

    JScript, VBScript, 42 bytes

    rem=1;a="abc"/*
    a="cba"'*/
    WScript.echo(a)

    peter ferrie

    Posted 2017-08-09T18:24:55.367

    Reputation: 804

    0

    C / ><>, 33 bytes

    #//;ooo"cba"
    main(){puts("abc");}
    

    What C sees

    Try it online!

    #
    ​

    Begin empty preprocessor directive.

    //;ooo"cba"
    ​

    Single-line comment.

    
    main(){puts("abc");}

    Print abc.

    What ><> sees

    Try it online!

    #
    ​
    Reflect the code in the other direction.

           "cba"
    ​

    String literal. Push the character codes of abc to the stack in order.

        ooo
    ​

    Output each of those characters. Since c is at the top of the stack, it is output first.

       ;
    ​

    End the program.

     //
    main(){puts("abc");}
    

    Never executed.

    Esolanging Fruit

    Posted 2017-08-09T18:24:55.367

    Reputation: 13 542

    0

    ><> and Underload, 14 bytes

    "(abc)S"}}ooo;
    

    To ><>:

    Try it online!

    "(abc)S"
    

    String literal. Push the character codes of (abc)S to the stack.

            }}
    

    Move the TOS to the bottom twice.

              ooo
    

    Output three characters (cba).

                 ;
    

    Halt.

    To Underload:

    https://tio.run/##K81LSS3KyU9M@f9fSSMxKVkzWKm2Nj8/3/r/fwA

    "
    

    Unrecognized

     (abc)
    

    Push the string abc.

          S
    

    Output it.

           "}}ooo;
    

    More unrecognized characters.

    Esolanging Fruit

    Posted 2017-08-09T18:24:55.367

    Reputation: 13 542

    0

    Javascript and Japt, 18 15 bytes

    $Pw=P="cba";$Pw
    

    Outputs "abc" in Japt and "cba" Javascript.

    Try it online!

    Nit

    Posted 2017-08-09T18:24:55.367

    Reputation: 2 667

    0

    Add++/Whispers v2, 29 bytes

    x:"abc"
    O
    > "cba"
    >> Output 1
    

    Try Add++ online!

    Try Whispers online!

    This exits with an exit code of 1 when run in Add++.

    How it works

    Add++

    Add++ goes through each line in a linear fashion, executing as it does so. The first line

    x:"abc"
    

    sets the x variable to the string abc. Then

    O
    

    outputs that string. Next

    > "cba"
    >> Output 1
    

    (the Whispers code), compares the strings abc and cba lexicographically before throwing an error at the double >> on the fourth line.

    Whispers

    Whispers removes any lines which don't match one of it's five regexes, all of which only match lines beginning with >, making the code processed by Whispers

    > "cba"
    >> Output 1
    

    Whispers' execution model is almost the exact opposite of Add++; it begins at the bottom line, and calls the lines indicated by the numbers as arguments.

    >> Output 1
    

    calls the function stored on line 1, then outputs the result. Line 1 is

    > "cba"
    

    , a nilad line which returns a single value and takes no arguments. Here, it returns the string cba, which is then outputted by the last line.

    user80061

    Posted 2017-08-09T18:24:55.367

    Reputation:

    0

    PHP & HTML, 15 bytes

    <?die(abc)?>cba
    

    PHP exits with printing abc; cba is ignored.
    HTML sees the PHP code as unrecognized tag (and ignores it), then prints cba.

    Titus

    Posted 2017-08-09T18:24:55.367

    Reputation: 13 814

    0

    Python 3 and Batch, 24 bytes

    print('abc')#|echo cba
    

    This program just takes advantage of operators in both languages.

    What Python Sees

    print('abc') # This is a comment, do not run

    What Batch Sees

    oh noes! errors! run this -> echo cba

    Mercury Platinum

    Posted 2017-08-09T18:24:55.367

    Reputation: 161

    0

    Tcl, 79 bytes

    #include<stdio.h>/*
    proc M {} {puts cba}
    #*/
    #define M main(){printf("abc");}
    M
    

    Try it online!


    C (gcc), 79 bytes

    #include<stdio.h>/*
    proc M {} {puts cba}
    #*/
    #define M main(){printf("abc");}
    M
    

    Try it online!

    sergiol

    Posted 2017-08-09T18:24:55.367

    Reputation: 3 055