57
\$\begingroup\$

Your task, if you wish to accept it, is to write a program that outputs a non-zero number(can be integer or float). The tricky part is that if I reverse your source code, the output must be the original integer negated.

Rules

  • You must build a full program. That is, your output has to be printed to STDOUT.

  • Both the numbers must be in base 10 (outputting them in any other base or with scientific notation is forbidden).

  • Outputting the numbers with trailing / leading spaces is allowed.

  • This is code-golf, so the shortest (original) code in each language wins!

  • Default Loopholes apply.

Example

Let's say your source code is ABC and its corresponding output is 4. If I write CBA instead and run it, the output must be -4.

\$\endgroup\$
3
  • 9
    \$\begingroup\$ If we could reverse at the bit level rather than the byte level the one byte - (0x45 = 0b00101101) works in Jelly - - yields -1 since it defines the literal -1, while (0xB4 = 0b10110100) yields 1 since it performs a logical not of the implicit input of zero. (Of course works just as well :p) \$\endgroup\$ Sep 17, 2019 at 21:23
  • \$\begingroup\$ @TwilightSparkle Does "have an unused, empty input" mean we can rely on input functions immediately reaching EOF? \$\endgroup\$
    – user58288
    Sep 18, 2019 at 12:56
  • 1
    \$\begingroup\$ @Borka223 No, you may not. \$\endgroup\$ Sep 18, 2019 at 13:44

72 Answers 72

3
\$\begingroup\$

Poetic, 153 bytes

mood o-o level a
was i redder?o,rats
lived on,lager:a trap!no reviver
ah hah ha
god deified on?now i strap on
redder was i?redder i saw,o
sleep o-on time

Try it online!

Prints 1.

Reversed

emit no-o peels
o,was i redder?i saw redder
no parts i won?no deified dog
ah hah ha
reviver on!part a:regal,no devil
star,o?redder i saw
a level o-o doom

!enilno ti yrT

Prints -1.

\$\endgroup\$
1
  • \$\begingroup\$ Wow! Point is, they are still words when reversed! \$\endgroup\$ Aug 29, 2020 at 6:50
2
\$\begingroup\$

Python 3, 18 bytes

print(1)#)1-(tnirp

Try it online!

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

MarioLANG, 5 bytes

+
:
-

Try it online!

Reversed:

-
:
+

Try it online!

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

Stax, 2 bytes

U1

Run and debug it at staxlang.xyz!

!zyx.gnalxats ta ti gubed dan nuR

Push -1. Push 1. Implicitly print the top of the stack.

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

Turing Machine Language, 29 bytes

0 * - r 1
1 * 1 * 1
1 r _ * 0

And its reverse:

0 * _ r 1
1 * 1 * 1
1 r - * 0

Try it online!

Note: This will run in a continous loop both ways.

Explanation:

0 * - r 1     ; Prints a hyphen '-' then moves right along the tape.
1 * 1 * 1     ; Prints '1' and loops back in on itself.
1 r _ * 0     ; Never hits.

And its reverse:

0 * _ r 1     ; Prints a space then moves right along the tape.
1 * 1 * 1     ; Prints '1' and loops back in on itself.
1 r - * 0     ; Never hits.
\$\endgroup\$
2
\$\begingroup\$

JavaScript, 31 bytes

The obligatory comment abuse for JavaScript!

console.log(1)//)1-(gol.elosnoc

and reversed:

console.log(-1)//)1(gol.elosnoc
\$\endgroup\$
2
\$\begingroup\$

><>, 7 bytes

A solution using the stack length. The regular and reversed programs share most of the code.

2l-n;<3

Try it online!
!enilno ti yrT


><>, 7 bytes

A more boring solution where the programs only share the ;.

1n;n-10

Try it online!
!enilno ti yrT


If we were allowed to use the fact that stdin is empty, this 5-byte variation of the second solution would be legal. Alas, it is not.

1n;ni

Try it online!
!enilno ti yrT

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

\$\Large{\mathbf{\TeX} \left(\text{MathJax}\right)},~4\,\text{bytes}\$

1%1-

$$ \begin{array}{|r|c|c|} \hline & \textbf{Code} %& \begin{array}{c}\boxed{\textbf{Boxed}} \\ \textbf{Result} \end{array} & \textbf{Result} \\\hline \textbf{Forward:} & \texttt{1%1-} & %\boxed{ 1%1- %} \\\hline \textbf{Backward:} & \texttt{-1%1} & %\boxed{ -1%1 %} \\\hline \end{array} $$

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

PHP, 10 bytes.

Not very special, but it's rare for PHP to be short :)

<?=1#1-=?<
\$\endgroup\$
2
  • 1
    \$\begingroup\$ I haven't tested this, but I suspect the last > should be <. \$\endgroup\$ Sep 21, 2019 at 5:38
  • \$\begingroup\$ This doesn't work when reversed. And with > changed to < it won't work at all because of "unexpected end of file" error. It would need semicolons to work which will be same as the version already posted in this post: codegolf.stackexchange.com/a/192982/81663 \$\endgroup\$
    – Night2
    Sep 21, 2019 at 11:58
2
\$\begingroup\$

International Phonetic Esoteric Language, 8 6 bytes (WIP language) (OLD)

No TIO interpreter yet, but is runnable by cloning the repository above, and calling python main.py "code here".

Forward, prints 1:

od12do

Backward, prints -1:

od21do

If instructions try to peek or pop at an empty stack, they are noped. In this case, instructions o, ɲ, and d turn into nops when the code is run, depending on where they are.

Forward:

oɲd12dɲo
o      ; nop
 d     ; nop
  1    ; push 1
   2   ; push 2
    d  ; pop, pop, push [2 - 1]
     o ; pop, print

Backward:

oɲd21dɲo
o      ; nop
 d     ; nop
  2    ; push 2
   1   ; push 1
    d  ; pop, pop, push [1 - 2]
     o ; pop, print

-2 bytes for not swapping the stack.

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

MAWP 0.1, 18 bytes

?.:M?%?00?;W590?.?

Displays 1. Try it!

?.?095W;?00?%?M:.?

Displays '-' and then 1, which shows -1. Try it!

\$\endgroup\$
3
  • \$\begingroup\$ Nice answer, +1. You are answering with MAWP 0.1 everywhere recently. \$\endgroup\$ Aug 6, 2020 at 7:49
  • \$\begingroup\$ your link is broken \$\endgroup\$
    – Jo King
    Aug 31, 2020 at 2:23
  • \$\begingroup\$ All MAWP links are broken since Dion rerouted the website. It was too much manual work, so I've left most of my answers as is. You can test this here: 8dion8.github.io/MAWP \$\endgroup\$
    – Razetime
    Aug 31, 2020 at 2:36
2
\$\begingroup\$

Arn, 3 bytes

Forward:

1-0

Backward:

0-1

Pretty simple, I'm looking for a more interesting answer now

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

Vyxal, 2 bytes

-1

Try it Online!

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

HBL, 1 byte

+<

Run forwards, outputs 4:

+   Increment
 <  Three

Run backwards, outputs -4:

<   Negate
 +  Four

Found while exploring different answers to Am I being run backwards?, which is a superset of this question.

\$\endgroup\$
1
  • \$\begingroup\$ One of the not-so-trivial answers to this trivial challenge! \$\endgroup\$ Aug 11, 2022 at 11:56
2
\$\begingroup\$

Knight, 4 bytes

O1~O

Try It Online!

Try It Online reversed!

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

convey, 8 7 bytes

-1 byte if writting it horizontaly and not verticaly, (you save one line feed)

Prints -1 and 1

1-0
[}[

Try it online! (old version)

[}[
0-1

Try it online! (old version)

I wonder if there is someway to work whit the direction of the conveyor when you reverse the code, i have been trying to do something like that but it changes also the output position } and the code doesn't compile :(

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Another of the not-so-trivial answers to this trivial challenge! \$\endgroup\$ Aug 11, 2022 at 11:56
1
\$\begingroup\$

Ruby, 8 7 bytes

p 1#0~p

Try it online!

Thanks Arnauld for chipping off 1 byte and making it less dull.

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

Charcoal, 5 bytes

ωP¹1←

Try it online! Outputs 1. Explanation:

ω   Print the empty string (does nothing)
P¹ Print a `-` without moving the cursor
1   Print a `1`
←   Move the cursor left (no effect on output)

Reversed:

←1¹Pω

Try it online! Outputs -1. Explanation:

←1  Print a `1` and move the cursor left
¹   Print a `-`
Pω  Print the empty string (does nothing)
\$\endgroup\$
1
\$\begingroup\$

Brachylog, 4 bytes

wṅ1?

Try it online!

Prints the positive output with a leading zero. The leading zero is actually just a zero that gets printed separately, but...

w       Print the input variable.
 ṅ      Something negated
  1     is 1,
   ?    which is the input variable.

!enilno ti yrT

?       The input variable is the input variable,
 1      which is 1.
   w    Print it
  ṅ     negated.
\$\endgroup\$
1
\$\begingroup\$

Stax, 5 bytes

1PsPU

Not going to win any awards, but this one is refreshingly straightforward. Depends on input being empty.

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

Forth (gforth), 10 8 bytes

1 . -1 0

Outputs: 1

Reverse:

0 1- . 1

Outputs: -1

Try it online!

Code Explanation

1     \ Place 1 on the stack
.     \ Output top of stack
-1 0  \ Place -1 and 0 on the stack but don't output them

Reverse:
0 1-  \ Place 0 on the stack and subtract 1
.     \ Output top of stack
1     \ Place 1 on stack, but don't output it
\$\endgroup\$
1
\$\begingroup\$

Vim, 6 keystrokes/bytes

<C-x><C-x><esc>1i1

Outputs 1. Try it online!

1i1<esc><C-x><C-x>

Outputs -1. Try it online!

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

Keg, 5 bytes

1#-10

Explanation:

1#    Push 1
 #-10 Comment

Reversed explanation:

01-#  Push -1
   #1 Comment
\$\endgroup\$
1
\$\begingroup\$

GolfScript, 4 bytes

Simply abusing comments to remove a number.

1#1-

TIO

Reversed:

-1#1

TIO

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

Pip, 3 2 bytes

vo

o is a variable denoting 1, v is -1.

0-1 is another 3 byte solution, but since it was already made in Python, I decided to make this instead.

-1 byte from Dlosc.

Try it online!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ You are learning Pip. Wow. Something I always wanted to learn but haven't time. \$\endgroup\$ Aug 29, 2020 at 5:47
  • 1
    \$\begingroup\$ It is surprisingly easy. \$\endgroup\$
    – Razetime
    Aug 29, 2020 at 5:47
  • \$\begingroup\$ The newline isn't necessary, so you can officially beat Python. ;) \$\endgroup\$
    – DLosc
    Aug 30, 2020 at 19:06
  • \$\begingroup\$ I was initially using 1 and -1 instead of v and o. silly me. \$\endgroup\$
    – Razetime
    Aug 31, 2020 at 1:12
1
\$\begingroup\$

Vyxal, 2 bytes

u1

Try it Online!

!enilnO ti yrT

1 pushes 1, u pushes -1. The last one pushed is automatically printed at the end of execution.

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

Fueue, 9 bytes

(H)!45 54

Outputs 6.

Try it online!

Reversed

45 54!)H(

Outputs -6.

!enilno ti yrT

Explanation

Fueue is a queue-based language where the program is stored in a queue. (The program queue is also the only way to store data.) From Esolang:

Evaluation proceeds as follows:

  • Look at the front element of the queue.
  • If it is a numeric value, convert it to a character and print.
  • If it is a function, and there are enough values of the appropriate type directly behind it in the queue, the function and those values are popped, and the result is pushed back to the end of the queue.
  • Otherwise, it is simply sent to the back of the queue.

(There is another bullet point on the Esolang page, but it is irrelevant for this answer.)

I take advantage of the fact that the decimal representation of the ASCII code for 6, which is 54, is the reverse of the ASCII code for -, which is 45.

In my explanations, I will be listing out the steps to evaluate each program, skipping a few obvious steps. The explanation to the right of a queue is how to get from the previous queue to the current queue, not how to get from the current queue to the next queue.

Normal

QUEUE EXPLANATION
( H ) ! 45 54 Starting code.
) ! 45 54 [H] Put H in a block. (Note that the results of an operation are put at the end of the queue.)
! 45 54 [H] ) Can’t be executed, so moved to end of queue.
54 [H] ) Pop 45.
[H] ) Output "6".
) [H] Blocks can’t be executed normally.
H Unblock [H].
Halt.

Reversed

QUEUE EXPLANATION
45 54 ! ) H ( Starting code.
! ) H ( Output "-6".
H ( Pop ).
Halt.
\$\endgroup\$
1
\$\begingroup\$

Rust, 50 bytes

fn main(){print!("2")}//
//})"2-"(!tnirp{)(niam nf

Attempt This Online! or !enilnO sihT tpmettA

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

Perl 5 + -p0513l061, 5 bytes

#-##s

Try it online!

s##-#

!enilno ti yrT

Explanation

Typical flag abuse. -p provides and implicit read/print while loop, -0513 sets $/ to undef (which enters slurp mode and will execute the code even without any value in STDIN) and -l061 sets the output record separator ($\) to 1. The initial form is just commented out code with #, so $_ is empty and $\ is 1 so the output is 1. The reversed form is a s///ubstitution using # as the delimiter replacing nothing with -, resulting in the output of -1 because of $\.

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

MathGolf, 3 bytes

d;3

Boring approach for now. Will see if a 2-byter is possible.

Outputs 3/-3. c;2 and b;1 are two alternatives for 2/-2 and 1/-1 respectively.

Try it online or try it online reversed.

Explanation:

d    # Push builtin -3
 ;   # Discard the top of the stack
  3  # Push 3
     # (output the entire stack joined together implicitly)
\$\endgroup\$
4
  • \$\begingroup\$ Why does MathGolf have a builtin for -3? \$\endgroup\$ Sep 17, 2019 at 14:34
  • 1
    \$\begingroup\$ @TwilightSparkle I have absolutely no idea.. XD It has way too many 1-byte number builtins tbh.. The ones available are: -3 through and including 38; 60; 64; 100; 128; 256; 512; 1000; 1024; 2048; 3600; 4096; 10000; 86400; 100000; 1000000; 10000000; 100000000. As well as PI, 2*PI, golden ratio, [0], [1], etc.. All single-byte builtins.. In the mean time it's missing a simple 1-byte negate. ;) \$\endgroup\$ Sep 17, 2019 at 14:39
  • \$\begingroup\$ Why not a built in for the Ultimate Answer(42), like AlephAlpha's esolang, Samau? \$\endgroup\$ Sep 18, 2019 at 4:29
  • \$\begingroup\$ @TwilightSparkle Like I said, I have no idea why there are so many number builtins to begin with. ;) You should ask maxb who created the language. \$\endgroup\$ Sep 18, 2019 at 7:10

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.