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
    Commented Aug 9, 2017 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
    Commented Aug 9, 2017 at 20:04
  • 2
    \$\begingroup\$ Can you print ABC instead of abc \$\endgroup\$
    – Oliver Ni
    Commented Aug 10, 2017 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
    Commented Aug 10, 2017 at 2:37
  • 2
    \$\begingroup\$ 'ABC' and 'CBA' is ok, leading/trailing whitespace/newlines ok but must be the same for both outputs \$\endgroup\$ Commented Aug 10, 2017 at 7:56

64 Answers 64

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

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ý

\$\endgroup\$
2
  • \$\begingroup\$ Would it be possible to remove the space after #include, since it's read as ;include? (Or at least from your explanation) \$\endgroup\$
    – Adalynn
    Commented Aug 11, 2017 at 11:23
  • \$\begingroup\$ @Zacharý, yes, thanks! \$\endgroup\$
    – Renzo
    Commented Aug 11, 2017 at 19:07
2
\$\begingroup\$

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.

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

Japt/Japt 2.0, 14 bytes

;AÊ?"abc":"cba

Japt outputs cba

Japt 2.0 outputs abc

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

MAWP / 1+, 71 bytes

1%(|"+1%+)1%()"+"+"+()();1%()"+"+"+()"+;1%()"+"+"+"+();77W2W1A!!;1M;2M;

Uses the % operator to remove all the stray 1's from the 1+ program. Rounded brackets are immediately skipped since there is no 0 in the stack for MAWP.

Try it! (MAWP)

Try it Online! (1+)

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

JavaScript (V8), 65 bytes

console.log(parseInt('abc--[----->+<]>---.-.-.',16).toString(16))

Try it online!

brainfuck, 65 bytes

console.log(parseInt('abc--[----->+<]>---.-.-.',16).toString(16))

Try it online!

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

Ruby / C, 26 20 bytes

puts(0?"abc":"cba");

-6 from Dom Hastings.

Try it online! (Ruby)

Try it online! (C)

0 is a truthy value in Ruby, so it prints "abc", and C prints "cba".

puts is a function that works in both languages.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ You can save 6 bytes with puts(0?"abc":"cba");! \$\endgroup\$ Commented Aug 17, 2020 at 11:27
  • \$\begingroup\$ I assumed that the ternary operator didn't work properly with strings in C. Yeesh. \$\endgroup\$
    – Razetime
    Commented Aug 17, 2020 at 11:35
2
\$\begingroup\$

Braingolf and Lean Mean Bean Machine, 35 bytes

 O
""
#a
!!
""
#b
!!
""
#c
& ,&
@U;

Braingolf: Try it online!
LMBM: Try it online!

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. Thankfully in braingolf v1, modifiers are only held through no-ops, meaning they will be dropped when braingolf hits the empty string, which doesn't use modifiers (unlike v2 where they are held until used) so we don't have to make sure to consume that modifier before we try to print the stack.

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 through no-ops such as the space, 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.

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

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!

\$\endgroup\$
3
  • 3
    \$\begingroup\$ What is the purpose of posting another answer which is even more verbose than your first one? \$\endgroup\$
    – user72349
    Commented Aug 9, 2017 at 18:41
  • 1
    \$\begingroup\$ @ThePirateBay It is a completely different approach \$\endgroup\$
    – Mr. Xcoder
    Commented Aug 9, 2017 at 18:42
  • \$\begingroup\$ @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. \$\endgroup\$
    – hyperneutrino
    Commented Aug 9, 2017 at 18:42
1
\$\begingroup\$

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!

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

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!

\$\endgroup\$
3
  • \$\begingroup\$ Oh, thanks for having that as a note, I need to modify the MY interpreter now. And nice golf too! \$\endgroup\$
    – Adalynn
    Commented Aug 9, 2017 at 23:55
  • \$\begingroup\$ @Zacharý is there any good reason to still support Python 2? :P \$\endgroup\$
    – Stephen
    Commented Aug 10, 2017 at 0:21
  • \$\begingroup\$ Yes. Because people still use Python2 (including me, since I have nltk and sympy on Py2) \$\endgroup\$
    – Adalynn
    Commented Aug 10, 2017 at 1:03
1
\$\begingroup\$

JavaScript (ES8 & ES6), 24 bytes

_=>"".padEnd?"abc":"cba"
\$\endgroup\$
3
  • \$\begingroup\$ What about ES5 vs ES6 test [].keys \$\endgroup\$
    – tsh
    Commented Aug 10, 2017 at 2:46
  • \$\begingroup\$ Or even ES5 vs ES3 test [].map... \$\endgroup\$
    – tsh
    Commented Aug 10, 2017 at 2:52
  • \$\begingroup\$ @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. \$\endgroup\$
    – Shaggy
    Commented Aug 11, 2017 at 8:28
1
\$\begingroup\$

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)

\$\endgroup\$
0
1
\$\begingroup\$

C/><>, 58 bytes

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

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.

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

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.

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

C and x86 DOS binary, 58 bytes

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

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)                |
\$\endgroup\$
1
  • 2
    \$\begingroup\$ I don't think this is valid unless you can provide a hexdump. \$\endgroup\$ Commented Apr 22, 2018 at 7:31
1
\$\begingroup\$

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.

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

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

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

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

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

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!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ i knew someone was gonna (ab)use comment quirks \$\endgroup\$
    – mekb
    Commented Jul 7, 2021 at 9:07
  • \$\begingroup\$ @mekb If you see other answers, lots of people are doing the same. \$\endgroup\$
    – sergiol
    Commented Jul 7, 2021 at 13:12
  • \$\begingroup\$ this was the first i saw \$\endgroup\$
    – mekb
    Commented Jul 8, 2021 at 9:52
1
\$\begingroup\$

Pxem (ぬこ's intended specification) and Pxem (pxemi.7z: his actual implementation), 16 bytes.

Filename:

.wcba.p.d.aabc.p

Content is empty.

What it does

ぬこ, the designer of Pxem, originally intended that .w should NOT get into the loop when stack is empty, but his reference implementation does not satisfy it: it gets into the loop when stack is empty. This is why none of we, implementers, have ever complied his intended specification at all.

\$\endgroup\$
0
\$\begingroup\$

JScript, VBScript, 42 bytes

rem=1;a="abc"/*
a="cba"'*/
WScript.echo(a)
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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