41
\$\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\$
  • 6
    \$\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\$ – Jonathan Allan Sep 17 '19 at 21:23
  • \$\begingroup\$ @TwilightSparkle Does "have an unused, empty input" mean we can rely on input functions immediately reaching EOF? \$\endgroup\$ – Borka223 Sep 18 '19 at 12:56
  • 1
    \$\begingroup\$ @Borka223 No, you may not. \$\endgroup\$ – HighlyRadioactive Sep 18 '19 at 13:44
  • \$\begingroup\$ @JoKing Copying. I will remove that. \$\endgroup\$ – HighlyRadioactive Sep 19 '19 at 4:16

55 Answers 55

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

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

PHP, 10 bytes.

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

<?=1#1-=?<
\$\endgroup\$
  • 1
    \$\begingroup\$ I haven't tested this, but I suspect the last > should be <. \$\endgroup\$ – Ørjan Johansen Sep 21 '19 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 '19 at 11:58
1
\$\begingroup\$

Python 3, 18 bytes

print(1)#)1-(tnirp

Try it online!

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

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

Befunge-93 (FBBI), 7 6 bytes

1-.@.1

-1 byte thanks to @squid.

Try it online!

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

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

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\$
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\$
  • \$\begingroup\$ Why does MathGolf have a builtin for -3? \$\endgroup\$ – HighlyRadioactive Sep 17 '19 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\$ – Kevin Cruijssen Sep 17 '19 at 14:39
  • \$\begingroup\$ Why not a built in for the Ultimate Answer(42), like AlephAlpha's esolang, Samau? \$\endgroup\$ – HighlyRadioactive Sep 18 '19 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\$ – Kevin Cruijssen Sep 18 '19 at 7:10
0
\$\begingroup\$

C (clang), 39 bytes

main(){puts("1");}//};)"1-"(stup{)(niam

Try it online!

Corrected as @AdamBorkBork error spotting.

\$\endgroup\$
  • 1
    \$\begingroup\$ Darn! You beat me by... 4 hours. \$\endgroup\$ – S.S. Anne Sep 17 '19 at 19:04
0
\$\begingroup\$

Gaia, 4 bytes

2-”2

Try it online!

2-”	| push the string "2-"
   2	| push 2
	| implicit print TOS
2”	| push the string "2"
  -2	| push -2
	| implicit print TOS
\$\endgroup\$
0
\$\begingroup\$

Putt, 3 2 bytes

N1
  1. NEGATE
  2. PUSH 1
  3. Prints implicitly

Reverse:

1N
  1. PUSH 1
  2. NEGATE
  3. Prints implicitly
\$\endgroup\$
0
\$\begingroup\$

Python REPL, 4 bytes

1-0

Only works in the Python REPL environment.

Shaved one byte based on suggestion from Ilmari Karonen.

\$\endgroup\$
  • \$\begingroup\$ Welcome to the site! As this only works in Python REPL, I've edited the language to reflect that. \$\endgroup\$ – caird coinheringaahing Sep 18 '19 at 19:30
  • \$\begingroup\$ Thanks, I appreciate that! \$\endgroup\$ – bob Sep 18 '19 at 19:48
  • \$\begingroup\$ Wouldn't 1-0 be one byte shorter? \$\endgroup\$ – Ilmari Karonen Sep 18 '19 at 21:49
  • \$\begingroup\$ It would, yes. I'll update it. \$\endgroup\$ – bob Sep 19 '19 at 12:36
0
\$\begingroup\$

Bash, 16 bytes

echo -1 # 1 ohce

Try it online!

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

dc, 4 bytes

1p1_

prints 1

in reverse,

_1p1

prints -1

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

ink, 6 bytes

1//1-\

Try it online!

Reversed

\-1//1

Try it online!

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

GolfScript, 4 bytes

Simply abusing comments to remove a number.

1#1-

TIO

Reversed:

-1#1

TIO

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