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

2
\$\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\$
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\$
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
    \$\begingroup\$ What is the purpose of posting another answer which is even more verbose than your first one? \$\endgroup\$ – user72349 Aug 9 '17 at 18:41
  • 1
    \$\begingroup\$ @ThePirateBay It is a completely different approach \$\endgroup\$ – Mr. Xcoder Aug 9 '17 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 Aug 9 '17 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\$
  • \$\begingroup\$ Oh, thanks for having that as a note, I need to modify the MY interpreter now. And nice golf too! \$\endgroup\$ – Zacharý Aug 9 '17 at 23:55
  • \$\begingroup\$ @Zacharý is there any good reason to still support Python 2? :P \$\endgroup\$ – Stephen Aug 10 '17 at 0:21
  • \$\begingroup\$ Yes. Because people still use Python2 (including me, since I have nltk and sympy on Py2) \$\endgroup\$ – Zacharý Aug 10 '17 at 1:03
1
\$\begingroup\$

JavaScript (ES8 & ES6), 24 bytes

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

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.

\$\endgroup\$
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\$
  • \$\begingroup\$ 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. \$\endgroup\$ – aschepler Aug 11 '17 at 22:27
  • 1
    \$\begingroup\$ There's a meta post saying that that isn't necessary, IIRC. \$\endgroup\$ – Zacharý Aug 11 '17 at 22:29
1
\$\begingroup\$

C/><>, 58 bytes

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

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

\$\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 / 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\$
  • \$\begingroup\$ Would it be possible to remove the space after #include, since it's read as ;include? (Or at least from your explanation) \$\endgroup\$ – Zacharý Aug 11 '17 at 11:23
  • \$\begingroup\$ @Zacharý, yes, thanks! \$\endgroup\$ – Renzo Aug 11 '17 at 19:07
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\$
  • 2
    \$\begingroup\$ I don't think this is valid unless you can provide a hexdump. \$\endgroup\$ – Esolanging Fruit Apr 22 '18 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\$

Japt/Japt 2.0, 14 bytes

;AÊ?"abc":"cba

Japt outputs cba

Japt 2.0 outputs abc

\$\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\$
0
\$\begingroup\$

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.

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

Javascript and Japt, 18 15 bytes

$Pw=P="cba";$Pw

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

Try it online!

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

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.