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\$ Commented 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
    Commented Sep 18, 2019 at 12:56
  • 1
    \$\begingroup\$ @Borka223 No, you may not. \$\endgroup\$ Commented Sep 18, 2019 at 13:44

72 Answers 72

1 2
3
0
\$\begingroup\$

C (clang), 39 bytes

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

Try it online!

Corrected as @AdamBorkBork error spotting.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Darn! You beat me by... 4 hours. \$\endgroup\$
    – S.S. Anne
    Commented Sep 17, 2019 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\$
4
  • \$\begingroup\$ Welcome to the site! As this only works in Python REPL, I've edited the language to reflect that. \$\endgroup\$ Commented Sep 18, 2019 at 19:30
  • \$\begingroup\$ Thanks, I appreciate that! \$\endgroup\$
    – bob
    Commented Sep 18, 2019 at 19:48
  • \$\begingroup\$ Wouldn't 1-0 be one byte shorter? \$\endgroup\$ Commented Sep 18, 2019 at 21:49
  • \$\begingroup\$ It would, yes. I'll update it. \$\endgroup\$
    – bob
    Commented Sep 19, 2019 at 12:36
0
\$\begingroup\$

Bash, 16 bytes

echo -1 # 1 ohce

Try it online!

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

Befunge-93 (FBBI), 7 6 bytes

[email protected]

-1 byte thanks to @squid.

Try it online!

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

Pxem, filename: 6 bytes + content: 0 bytes = 6 bytes.

  • Filename: 1.p.1-
  • Content: empty

Try it online!

.p is until empty; do putchar pop; done. .1 is not a command; treated as a substring for literal.

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

M4, 10 bytes, with warning

1dnl lnd1-

Try it online!


M4, 16 bytes, clean output

1m4exit tixe4m1-

Try it online!

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

Rattle, 5 bytes

+p;p-

Try it online!

This is pretty basic. Forwards, it will increment then print 1 (p- is an uncalled function). Reverse, it will decrement then print -1 (p+ is an uncalled function).

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

<>^v, 3 bytes

(;)

Prints -1.

Reversed:

);(

Prints 1.

Explanation

In <>^v, the stack is initialized to [0] (only one element, the number 0) at the start of every program. ( decrements the top of the stack, ; prints the top of the stack and ) increments the top of the stack. So (;) turns 0 into -1, then prints it, then turns it back into 0. Its reverse, );(, turns 0 into 1, prints it, then turns it back into 0.

Normal version ((;))

Reversed ();()

\$\endgroup\$
1 2
3

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.