44
\$\begingroup\$

Blatant rip-off of a rip-off. Go upvote those!

Your task, if you wish to accept it, is to write a program/function that outputs/returns its integer input/argument. The tricky part is that if I reverse your source code, the output must be the original integer negated.

Examples

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

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

An input of 0 may give 0 or -0, however, if you do support signed zero, -0 should give 0.

\$\endgroup\$
5
  • 5
    \$\begingroup\$ Why do we need a copy of the same question? \$\endgroup\$
    – Christian
    Commented Sep 18, 2019 at 7:30
  • 5
    \$\begingroup\$ @Christian That one outputs a constant number (and its negation) whereas this one has to take input and return/negate it. A very different job in a lot of languages. \$\endgroup\$
    – Adám
    Commented Sep 18, 2019 at 7:32
  • 6
    \$\begingroup\$ A yes, now I see the difference. One needs to read VERY carefully \$\endgroup\$
    – Christian
    Commented Sep 18, 2019 at 7:34
  • \$\begingroup\$ If using a structured language like C#, are you just reversing lines? \$\endgroup\$
    – Taco
    Commented Sep 19, 2019 at 2:31
  • \$\begingroup\$ @PerpetualJ No, look at the source like list of characters, some of which are line breaks. \$\endgroup\$
    – Adám
    Commented Sep 19, 2019 at 5:42

69 Answers 69

1 2
3
0
\$\begingroup\$

Rattle, 12 bytes

||1c0gI|\\&-

Try it online!

!enilno ti yrT

This takes advantage of how Rattle is parsed. As of right now, there is only support for one input section (everything before the first |) - this means that anything after any following |'s will be disregarded. This more or less splits the code into two sections:

|

This is the effective forward code, which is the cat program.

-&\\|Ig0c1

This is the reverse code, which spits out the negative of the input. There's still possibility that this could be golfed further!

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

Pxem, Filename: 30 27 bytes + Content: 0 bytes = 30 27 bytes, does not support signed zero, not clean.

  • Filename (escaped unprintable): ._.n.o.-a.d.n.-.-.00y.\001c._.
    • Reversed: ._.c\001.y00.-.-.n.d.a-.o.n._.
  • Content: empty

Try it online!

!enilno ti yrT

With comments

Original

XX.z
.a._.nXX.z # push getint; putint pop
.a.o.-XX.z # DOTo and DOT- works like EOF for insufficient items
.aa.dXX.z # push ascii code of a; exit
.a.n.-.-.00y.\001c._.XX.z # deadcode
  ## NOTE this is syntaxically valid
.a

Reversed

XX.z
.a._.cXX.z # push getint; dup
.a\001.yXX.z # push 1; while pop>pop; do
  .a00.-.-XX.z # (*essentially*) push abs(0-pop)
    # NOTE actually: push 48; push 48; 
    #     push abs(pop-pop); push abs(pop-pop)
    # NOTE NULL char not supported for filename
  .a.n.dXX.z # putint pop; exit
.a.aXX.z # done
.a-.o.nXX.z # push hyphen; putchar pop; putint pop
.a._XX.z # this is why it aborts when only an integer is given
.a..c.sXX.z # push dot
.a

Pxem, Filename: 34 bytes + Content: 0 bytes = 34 bytes, clean.

  • Filename (escaped some): ._d..s.s.n.o.-a.d.n.-.-.00y.\001c._.
    • Reversed: ._.c\001.y00.-.-.n.d.a-.o.n.s.s..d_.
  • Content: empty

This version deals with exiting; reversed version exits well against non-negative input, too.

Try it online!

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

MMIXAL, 35 bytes

f POP 1,0 # 0,1 POP ;0$,0,0$ UGEN f

Very simple; when reversed, is just negate-and-return.

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

MMIX, 12 bytes (3 instrs)

00000000: f8010000 000001f8 00000036           ẏ¢¡¡¡¡¢ẏ¡¡¡6

Disassembled:

f   POP  1,0
    TRAP 0,Fopen,248
    TRAP 0,Halt,36

Reversed and disassembled:

f   NEGU $0,0,$0
    POP  1,0
    TRAP 0,Fopen,248

This is essentially the same as my MMIXAL answer, except that that relied on comments and reversed the source code, while this just relies on early return.

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

SNOBOL4 (CSNOBOL4), 38 bytes

	OUTPUT =INPUT;END;DNE;TUPIN-= TUPTUO	

Try it online!

Reversed

Anything after an END label is ignored. ; is an alternate newline delimiter which made this answer easier to think about.

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

dc, 5 bytes

dp-r0

Try it online!

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

Ly, 3 bytes

+0-

Try it online!

Forwards:

+    : compute "0 + STDIN"
 0   : push 0
  -  : subtract the top two stack entries
     : prints stack as a number automatically

Backwards:

-    : compute "0 - STDIN"
 0   : push 0
  +  : add the top two stack entries
     : prints stack as a number automatically
\$\endgroup\$
0
\$\begingroup\$

Knight, 5 bytes

O P~O

Try It Online!

Try It Online reversed!

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

Thunno, \$ 2 \log_{256}(96) \approx \$ 1.65 bytes

nI

Attempt This Online! or !enilnO sihT tpmettA

Outputs inside brackets, e.g. [5].

Port of jasanborn's 05AB1E answer. n = negate, I = push input.

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