-8
\$\begingroup\$

Objective

Write a program that outputs some printable ASCII characters. The trick is that if the program is reversed byte-by-byte, it must output the same thing in the reverse order, byte-by-byte, too.

Rules

  • No standard loopholes.
  • The program must not be a palindrome, nor can it be 1 byte in length (or empty). No restriction for patterns of the output as long as:
  • The output is at least 2 bytes long. It must contain solely printable ASCII characters, including visible characters and newlines, tabs and spaces. It should not be empty.
  • Outputs to standard error is ignored.
  • Quine programs are forbidden.

    For example, the following program makes no sense (HQ9+)

    AQ
    

Easy Mode

Paired parentheses/brackets/curly brackets are swapped after reversing source, so a reverse may be:

print("Hello");
;("olleH")tnirp  # not ;)"olleH"(tnirp

Make a note in your answer if your program uses this "Easy Mode".

Example

These are theoretical examples and may not exist in any programming language.

input -> output

program -> abcd
margorp -> dcba

() -> 10
)( -> 01

print('yes')tnirp -> yes
print('sey')tnirp -> sey

This is a , so the program with the shortest source code (in bytes) in each language wins!

\$\endgroup\$
  • 1
    \$\begingroup\$ Surely any cat-like language can do this in 2 bytes? \$\endgroup\$ – Neil Sep 15 '17 at 0:31
  • 13
    \$\begingroup\$ @iBug I think you should try sandbox next time. I feel that you just wanted to post your answer without taking sometime to think about other peoples answers (like quines). Also it is not nice to change the rules after 12 answers have been posted... \$\endgroup\$ – J42161217 Sep 15 '17 at 0:49
  • 2
    \$\begingroup\$ I see this put on hold but I do not see any comments about how this question can be improved. I'm voting to reopen. \$\endgroup\$ – Poke Sep 15 '17 at 1:21
  • 2
    \$\begingroup\$ I don't understand the last test case -- shouldn't it be sey output for the second part? \$\endgroup\$ – cole Sep 15 '17 at 2:29
  • 2
    \$\begingroup\$ Why is this challenge still getting downvoted? Please leave a comment if you think this challenge can be improved. \$\endgroup\$ – Poke Sep 15 '17 at 18:15

29 Answers 29

8
\$\begingroup\$

RProgN 2, 2 bytes

Ai

Forwards, produces ZYXWVUTSRQPONMLKJIHGFEDCBA, backwards, produces ABCDEFGHIJKLMNOPQRSTUVWXYZ.

A pushes the alphabet, and i reverses it, although if i doesn't have anything to reverse, it conveniently does nothing.

Try it online!

\$\endgroup\$
7
\$\begingroup\$

Jelly, 5 bytes

22«33

Try it online!

The first is min(22, 33) and the reverse is min(33, 22)

Also 5 bytes

.)25½

Try it online!

The ) seems to discard everything before it. This is sqrt(25) which is 5.0 and the reverse is . which is 0.5.

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

Rain-Flak, 65 59 bytes

((((((#)])([))(}{)}{))}{))
()()()()(
)){})){}){})())#((((((

Try it online!

This pushes


()

going forwards and


)(

going backwards.

It needs to pad the front with a newline because Brain-Flak's output automatically gets a newline added to the end of the output.

\$\endgroup\$
  • 2
    \$\begingroup\$ The language changed its name too? \$\endgroup\$ – Jonathan Allan Sep 15 '17 at 4:29
  • \$\begingroup\$ @JonathanAllan That is the (informal) name of the Ruby interpreter of the language. \$\endgroup\$ – Erik the Outgolfer Sep 15 '17 at 11:40
  • \$\begingroup\$ @JonathanAllan What Erik said is correct. The comment behavior used here is unique to the Ruby interpreter so I specified the interpreter as to avoid confusion. \$\endgroup\$ – Sriotchilism O'Zaic Sep 15 '17 at 17:32
3
\$\begingroup\$

Jelly, 2 bytes

0%

Try it online! ...or try %0

Both full programs output the palindromic nan as they both attempt to take a modulo of zero by zero and implicitly print the result.

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

Brain-Flak, 35 28+1=29 bytes

((#)}{))
()()()()(
 )){})#((

1 extra byte because of the flag -A to print ascii. This prints "\n\n"

Try it online!

-6 bytes thanks to Riley pointing out that I can cleverly use newlines in order to not duplicate as much code for the reversed program. This works because the pound sign only starts a single line comment.

\$\endgroup\$
  • \$\begingroup\$ When reversed, this does not produce the output reversed. \$\endgroup\$ – ATaco Sep 15 '17 at 0:34
  • 1
    \$\begingroup\$ Seems to print a space and a newline at the TIO link you provided. \$\endgroup\$ – Jonathan Allan Sep 15 '17 at 3:59
  • 1
    \$\begingroup\$ @JonathanAllan For some reason I thought 32 was a newline yesterday. Fixed to use 10 instead. \$\endgroup\$ – Poke Sep 15 '17 at 13:14
  • 1
    \$\begingroup\$ Saved quite a few bytes from that :D \$\endgroup\$ – Jonathan Allan Sep 15 '17 at 13:15
  • 2
    \$\begingroup\$ Combining this with Funky Computer Man's answer you can get to 30 bytes \$\endgroup\$ – Riley Sep 15 '17 at 13:39
3
\$\begingroup\$

Excel VBA, 11 Bytes

(Ab)uses the fact that Excel has more than one STDOUT; Does not use Easy Mode

Anonymous VBE immediate window function that takes no input and outputs 12 to the VBE immediate window.

?12'12=]1A[

Reversed

Anonymous VBE immediate window function that takes no input and outputs 21 to the the range [A1] on the ActiveSheet Object.

[A1]=21'21?

Note: ?12'12? works as well but feels a bit too close to palindromic to be within the spirit of the challenge

\$\endgroup\$
  • \$\begingroup\$ shouldn't [A1] be reversed to [1A]? \$\endgroup\$ – J42161217 Sep 15 '17 at 0:24
  • \$\begingroup\$ @Jenny_mathy Yep it should - and now it is \$\endgroup\$ – Taylor Scott Sep 15 '17 at 0:26
2
\$\begingroup\$

Pyth, 4 bytes

"Jj"

Outputs Jj and jJ when reversed.

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

Perl, 13 bytes

say+11#11 yas

And the reverse:

say 11#11+yas

Both print 11.

\$\endgroup\$
  • 2
    \$\begingroup\$ They both print 11␤. It seems upon my reading of the rules that newlines should be included. So it should be print not say \$\endgroup\$ – Brad Gilbert b2gills Sep 15 '17 at 0:48
  • \$\begingroup\$ You are right about the rules, but it would be better to replace the 11 with "^J" (where ^J stands for cntr-J, aka a new line (but a single byte)). We can then drop the space (or the +). The program then prints two new lines, and we will be using 14 bytes. \$\endgroup\$ – Abigail Sep 15 '17 at 14:36
2
\$\begingroup\$

Mathematica, 5 bytes

44+55

or...

Mathematica, 4 bytes

11*9

outputs 99

\$\endgroup\$
  • \$\begingroup\$ The last one is no longer allowed. Quines are forbidden. \$\endgroup\$ – iBug ......................... Sep 15 '17 at 0:44
  • 12
    \$\begingroup\$ @iBug I think you should try sandbox next time. I feel that you just wanted to post your answer without taking sometime to think about other peoples answers (like quines). Also it is not nice to change the rules after 12 answers have been posted... \$\endgroup\$ – J42161217 Sep 15 '17 at 0:49
2
\$\begingroup\$

Python 2, 43 42 52 bytes

Source

#1.,"n\"tnirp:tpecxe
#"""
print"\n",\
1.#"""cexe:yrt

Try it online!

Reversed source

try:exec"""#.1
\,"n\"tnirp
"""#
except:print"\n",.1#

Try it online!


Inspired by i cri everytim's answer. Does, however, not require 'easy mode'.

The program's outputs now are true reverses of one another, not ignoring the new line character. Thanks to @Jonathan Allan for pointing it out.

\$\endgroup\$
  • 1
    \$\begingroup\$ @JonathanAllan Thanks for noticing. \$\endgroup\$ – Jonathan Frech Sep 15 '17 at 4:12
2
\$\begingroup\$

Japt, 4 3 bytes

-1 byte thanks to @ETHproductions

Normal

12C

Try it online! Prints 12.

Reversed

C21

Try it online! Prints 21.

Only the last statement is printed, and C is the constant for 12.

\$\endgroup\$
  • \$\begingroup\$ Don't think you even need the ;, do you? \$\endgroup\$ – ETHproductions Sep 15 '17 at 12:48
  • \$\begingroup\$ @ETHproductions Right, I don't. I had been trying to implement using ; as the first char before coming to this one. \$\endgroup\$ – Justin Mariner Sep 15 '17 at 14:20
1
\$\begingroup\$

Jelly, 3 bytes

1+³

Try it online!

This is 1 + 100 which is 101. The reverse is just 100 + 1 which is also 101. Replacing + with _ works too.

Also 3 bytes

11»

Try it online!

The first is max(11, 0). The reverse is just max(0, 11). The argument is 0 by default if not present.

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

Python 2, 20 bytes

print'\n';#'n\'tnirp

Try it online!

print'\n' prints both the single newline it instructs and an implicit newline after.

print'\n'; does exactly the same - the ; is just there to stop the program being a palindrome.

The # marks the start of a comment.

\$\endgroup\$
  • \$\begingroup\$ 18 bytes if you return a number instead of a string -> if the implicit newline counts you could save even 2 more bytes from that answere. Or am I missing something? \$\endgroup\$ – Simon Sep 15 '17 at 7:45
  • \$\begingroup\$ @Simon print 12; does not "return a number" it prints a string representation of the number. The implicit newline needs to be a part of the reversed output, so you'd need to print'\n'+`12`;#... \$\endgroup\$ – Jonathan Allan Sep 15 '17 at 7:59
  • \$\begingroup\$ ...we could have print'';#''tnirp if it were not for the specification: "The output is at least 2 bytes long". \$\endgroup\$ – Jonathan Allan Sep 15 '17 at 8:01
  • \$\begingroup\$ Okay, I did read that reverse specification wrong.Would print 1;#'1n\'tnirpwork? \$\endgroup\$ – Simon Sep 15 '17 at 11:22
  • \$\begingroup\$ @Simon forwards that outputs 1\n and in reverse it outputs \n1\n. \$\endgroup\$ – Jonathan Allan Sep 15 '17 at 11:23
1
\$\begingroup\$

Charcoal, 2 bytes

The - is simply a string literal that implicitly prints itself. The ¹ is a numeric literal, and the default is to implicitly print one - too. Any ASCII character and non-zero Unicode superscript would work.

²←

Forwards, this prints two -s, then moves the cursor back one step (but this does not affect the output). Backwards, this prints two -s leftwards, but the output remains the same. Any subscript from 2 to 9 would work here, as would a right arrow or the reflect operator .

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

05AB1E, 4 bytes

?RA?

Try it online! or Try it reversed!

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

Japt, 2 bytes

Both output 11. B is the Japt constant for 11 and the w method, when applied to a number, returns the larger of that number and any number passed to it as an argument.

wB

Try it

Bw

Try it

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

Javascript (ES6), 12 bytes

_=>12//12>=_

Reversed:

_=>21//21>=_

Code snippet:

a=_=>12//12>=_
b=_=>21//21>=_
console.log(a())
console.log(b())

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

Java 8, 132 bytes

Dang full program requirements..

interface M{static void main(String[]a){System.out.print("OK");}}//}};)"OK"(tnirp.tuo.metsyS{)a][gnirtS(niam diov citats{M ecafretni

Try it here. Prints OK.

Reversed:

interface M{static void main(String[]a){System.out.print("KO");}}//}};)"KO"(tnirp.tuo.metsyS{)a][gnirtS(niam diov citats{M ecafretni

Try it here. Prints KO.

\$\endgroup\$
  • \$\begingroup\$ Hey! I said "make a note" if you're using the Easy Mode rule. \$\endgroup\$ – iBug ......................... Sep 16 '17 at 1:41
  • \$\begingroup\$ @iBug Oops.. misread that part. Roll-backed to my initial version. I edited on purpose so it wasn't the easy mode anymore; but apparently I've edited into the easy mode instead.. My initial answer was already not the easy mode.. My bad. \$\endgroup\$ – Kevin Cruijssen Sep 16 '17 at 15:08
1
\$\begingroup\$

Neim, 2 bytes

γδ

Outputs 12. Reversed, outputs 21.

Try it online!

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

SmileBASIC, 4 bytes

?12?

This will work in many BASIC dialects.

Prints "12" forwards, and "21" backwards.

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

brainfuck, 67 23 21 bytes

  • Saved two bytes thanks to 12Me21; using a tab instead of a new line.
+++++++++..+++++++++>

Try it online!

The program's ouput are two spaces new lines tabs. It is nearly a palindrome, apart from the trailing or leading cell shift (>).

\$\endgroup\$
  • \$\begingroup\$ Using tab (character 9) would save 2 more bytes. \$\endgroup\$ – 12Me21 Mar 1 '18 at 19:28
1
\$\begingroup\$

Triangular, 6 bytes

%.%%%%

Prints 000. Triangle looks like this:

  % 
 . % 
% % %

Reversed triangle looks like this:

  % 
 % % 
% . % 

The IP starts travelling southeast from the topmost character, so in both triangles %%% is interpreted.

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

Unix sh, 15 bytes

echo ab#ab ohce

Reversed:

echo ba#ba ohce

Outputs ab and ba, respectively.

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

shortC, 18 bytes

AJ"hi")}//})"hi"JA

Prints hi. Exploits comments. Try it online!

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

rk, 50 bytes

rk:start
dne:kr
print: "hi" :tnirp
trats:kr
rk:end

Reversed via this tool:

dne:kr
rk:start
print: "ih" :tnirp
rk:end
trats:kr

rk ignores everything before rk:start and rk:end. Of course, it ignores everything in the middle it doesn't understand anyway.

Try it online! vs Try it reversed!

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

Cubically 2.1, 5 bytes

%0%1%

Prints 09. Try it online! or Try it reversed!

The top (0-indexed) face is initialized to 0 and the left (1-indexed) face is initialized to 9. So printing (%) 0 prints 0 and printing 1 prints 9.

No longer works in Cubically 2.2 as % can be called implicitly.

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

Implicit, 2 bytes

À\

Same as the winning RProgN 2 answer. Try it online!

You can use a tool in this language to print the reverse of your code.

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

RProgN 2, 3 bytes

1
0

Output:

0
1

Try it online!


Reversed source:

0
1

Reversed output:

1
0

Try it online!

\$\endgroup\$
  • 6
    \$\begingroup\$ at least give other people a chance to answer your challenge before you post your own answers \$\endgroup\$ – HyperNeutrino Sep 15 '17 at 0:04
  • 3
    \$\begingroup\$ Is this the only thing anyone knows about RProgN2? It's a rather powerful language >:I \$\endgroup\$ – ATaco Sep 15 '17 at 0:37
  • \$\begingroup\$ @ATaco Yes. Someone else posted this twice on my questions, so it's the only program I can write. \$\endgroup\$ – iBug ......................... Sep 15 '17 at 0:38
  • 1
    \$\begingroup\$ Well it Has a tutorial if you ever feel like learning more about it! :P \$\endgroup\$ – ATaco Sep 15 '17 at 0:40

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.