41
\$\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
    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
    Sep 18, 2019 at 7:32
  • 6
    \$\begingroup\$ A yes, now I see the difference. One needs to read VERY carefully \$\endgroup\$
    – Christian
    Sep 18, 2019 at 7:34
  • \$\begingroup\$ If using a structured language like C#, are you just reversing lines? \$\endgroup\$ 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
    Sep 19, 2019 at 5:42

61 Answers 61

2
\$\begingroup\$

Stack Cats -mn, 2 bytes

-X

Try it online!

Try the reverse!

Explanation

Turns out this is actually a lot easier than the previous challenge in Stack Cats. The full program (after applying -m) here is -X-. X is used to swap the stacks left and right of the tape head, i.e. it doesn't affect the initial stack at all, so we can ignore it. But then the program is effectively just -- (negate the top of the stack twice), which does nothing.

For the inverse program, applying -m gives X-X. Again, X does nothing, so the program is effectively just -, which negates the top of the stack.

The only other 2-byte solution is -=, but it's virtually the same. The only difference is that = swaps only the tops of the adjacent stacks, not the entire stacks.

But again, using -m feels a bit like cheating, so below is a solution that uses a fully mirrored program.


Stack Cats -n, 7 bytes

:I<->I:

Try it online!

Try the reverse!

Explanation

The considerations from the previous answer still apply: any valid solution needs to use the paired characters and I. The six possible solutions (included in the TIO link) are all virtually the same. - and _ are equivalent in this program, and : can be replaced by | or T (which do the same for non-zero inputs and coincidentally also work for zero inputs). I've just picked this one to explain because it's easiest.

So remember that the initial stack holds the input on top of a -1 (on top of infinitely many zeros) whereas all the other stacks along the tape only hold zeros. Stack Cats also has the property that any even-length program does nothing (provided it terminates, but we can't use loops for this challenge anyway). The same is then obviously true for any odd-length program whose centre character does nothing... let's see:

:    Swap the input with the -1 below.
I    Move the -1 one stack to the left and turn it into +1.
<    Move another stack left (without taking the value).
-    Negate the zero on top of that stack (i.e. do nothing).

Therefore, the second half of the program exactly undoes the first half and we end up with the input on top of a -1 again.

The inverse program is :I>-<I:. Let's see how that changes things:

:    Swap the input with the -1 below.
I    Move the -1 one stack to the left and turn it into +1.
>    Move one stack right, i.e. back onto the initial stack which still holds the input.
-    Negate the input.
<    Move back to the left where we've parked the 1.
I    Move that 1 back onto the initial stack and turn it back into a -1.
:    Swap the -1 below the negated input to act as an EOF marker.
\$\endgroup\$
2
\$\begingroup\$

Brachylog, 2 bytes

&ṅ

Brachylog implicitly inputs from the left and outputs from the right.
& ignores anything to the left and passes the input to the function rightwards.
constrains each side of it to be negated versions of each other.

Try it online

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

Triangular, 8 7 bytes

$%%.|.$

Try it online!

Ungolfed:

   $           | $: Read an Integer
  % %          | %: Output it
 . | .
$

Reversed:

$.|.%%$

Try it online!

Previous Version (8 bytes):

$.%..|.$
\$\endgroup\$
2
\$\begingroup\$

W, 3 2 bytes

Pretty much a port of the Keg answer. Might get around to implement an implicit return.

%_

Explanation

   % Since there is nothing on-stack, return the input.
%_ % Start a comment

noitanalpxE

_  % Negate input & return
 % % Start a comment
\$\endgroup\$
1
\$\begingroup\$

Japt, 3 bytes

TnU

Try it | Reversed

Same method as my earlier solution, with T being 0 and U being the input.

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

V (vim), 5 bytes

É-ó--

Try it online!

Hexdump:

00000000: c92d f32d 2d                             .-.--
\$\endgroup\$
1
\$\begingroup\$

Stax, 2 bytes

pN

Run and debug it

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

Ruby -p , 7+1 = 8 bytes

#-?<<>$

Try it online!

Reverse:

Try it online!

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

PHP, 20 bytes

<?=$argn;#;ngra$-=?<

Try it online!

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

MathGolf, 4 bytes

*b╘k

Try it online or try it online reversed.

Explanation:

Regular:

*     # Multiply the (implicit) input with the (implicit) input
      #  STACK: [input**2]
 b    # Push -1
      #  STACK [input**2, -1]
  ╘   # Discard everything on the stack
      #  STACK: []
   k  # Push the input as integer
      #  STACK: [input]
      # (output the entire stack joined together as result)

Reversed:

k     # Push the input as integer
      #  STACK: [input]
 ╘    # Discard everything on the stack
      #  STACK: []
  b   # Push -1
      #  STACK: [-1]
   *  # Multiple the (implicit) input with this -1
      #  STACK: [-input]
      # (output the entire stack joined together as result)

And to answer your question: no, MathGolf does not have a 1-byte negate for integers. There is ~ for -n-1, but unfortunately nothing for -n (so *b could alternatively be )~ for the same byte-count).

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

Cubix, 5 bytes

@IO\n

Try it online!

Try it reversed!

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

Japt, 3 bytes

n U

Try it

Try it reversed

3 bytes

nÎN

Try it

Try it reversed

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

C++ (gcc), 29 bytes

#define f(x)x//x-)x(g enifed#

Try it online!

Fixed issue brought up by @Nick Kennedy

\$\endgroup\$
2
  • 1
    \$\begingroup\$ This is shown the wrong way round; it should return the input when called without reversing the code. \$\endgroup\$ Sep 18, 2019 at 10:51
  • \$\begingroup\$ Is #define f//- f enifed# (call as f x) cheating? ;) \$\endgroup\$
    – Quentin
    Sep 19, 2019 at 15:54
1
\$\begingroup\$

Runic Enchantments, 4 bytes

i@Zi

Try it online! Try it reversed!

Its the answer I originally wrote on the other challenge when I misread the description.

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

Gol><>, 4 bytes

Ih-I

One byte golfed off courtesy of JoKing

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ @JoKing Thank you very much, is that a negate operation? I honestly forgot that there was one \$\endgroup\$ Sep 18, 2019 at 23:43
  • \$\begingroup\$ @JoKing Ohhhhh, I didn't know that, thank you very much for enlightening me on that operation \$\endgroup\$ Sep 19, 2019 at 0:16
1
\$\begingroup\$

Scala, 6 bytes

+_//_-

Try it online!

Unfortunately a function can't just contain a _ to return the first argument, but using unary_+ will return the value unchanged.

Once flipped, the code is -_ which does a unary_- on the first argument, which flips the sign.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Shouldn't this be _//_-? However, _-0 seems like a valid solution. \$\endgroup\$
    – Adám
    Sep 18, 2019 at 18:45
  • \$\begingroup\$ @Adám It should be +_//_-. Unfortunately can't have _ as the body of the function, but doing +_ returns the value unchanged. Thanks for point it out! \$\endgroup\$
    – Soapy
    Sep 19, 2019 at 8:43
1
\$\begingroup\$

Perl 5 - 12 Bytes

say$_#_$-yas

Reversed

say-$_#_$yas

Abides by the -0 rule.

Does require the -nE flags on execution.

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

Retina 0.8.2, 13 12 bytes

-*$1#$
)^(|-

Try it online! It's impossible for the first line to match anything, so nothing happens. In reverse:

-|(^)
$#1$*-

Try it online! Explanation: Either a leading (implicitly) - or a leading empty string is matched. This is replaced with - repeated according to the number of empty strings that were matched.

Edit: Saved 1 byte thanks to @MartinEnder (although, cunningly, the reversed original worked with floating-point numbers in scientific notation). As he points out, Retina 1 would be a further byte shorter as its syntax for * is slightly different and does not require the adjacent $ (and it would therefore also modify illegal inputs such as -1# which this version ignores).

\$\endgroup\$
1
  • \$\begingroup\$ I don't think you need the ^ at the end. Also, Retina 1 would save a byte on $*. \$\endgroup\$ Sep 21, 2019 at 10:15
1
\$\begingroup\$

BitCycle -U, 15 bytes

First, a word about the -U flag. Internally, BitCycle only deals in 1's and 0's, so to allow working with decimal integers, it has flags to convert decimal input to unary. In particular, -U allows for signed integers by adding a 0 to the front of any nonpositive integer's unary representation: 4 is 1111, -4 is 01111, and 0 is 0. The same transformation is applied in reverse to the output, with the convenient addition that empty output is treated as 0.

Forward

^> 

?!+?
 <0/ 

Try it online!

The leftmost ? gets the input, which goes straight into the ! to be output unchanged.

Reverse

 /0< 
?+!?

 >^

Try it online!

The leftmost ? gets the input. The + sends the leading 0 bit, if any, left (north), and the 1 bits right (south).

If there is a leading 0 bit, it hits the splitter / and turns east, deactivating the splitter. The < sends it west again, through the deactivated splitter and off the playfield. The 0 bit that started on the playfield also hits the < and goes through the deactivated splitter and off the playfield. Meanwhile, the 1 bits are directed around by the > and ^ and reach the !, where they are output.

If there is no leading 0 bit, the 0 bit that starts on the playfield hits the < and goes west to the splitter /. Since the splitter has not been deactivated, it directs the bit south, and the + sends it west into the output !. Meanwhile, the 1 bits are directed to the output, with enough delay to make sure they get there after the 0 bit.

TL;DR: Nonpositive inputs have their leading 0 bit stripped and their magnitude is output. Positive inputs have a leading 0 bit added and are thus output as negative numbers of the same magnitude.

\$\endgroup\$
2
  • \$\begingroup\$ @JoKing That's clever! It's really different from mine; do you want to post it yourself? \$\endgroup\$
    – DLosc
    Sep 24, 2019 at 17:00
  • \$\begingroup\$ Posted \$\endgroup\$
    – Jo King
    Sep 24, 2019 at 21:54
1
\$\begingroup\$

BitCycle -U, 14 bytes

<  ^
v/?!
+~ +

Try it online!

And reversed

+ ~+
!?/v
^  <

Try it online!

The -U flag translates input/output to signed Unary, i.e. unary 1s with a zero in front if the number is negative. The first program just pipes input (?) to the output (!) directly right of it.

The reverse instead takes only the first bit of the number with the / command and dupnegs (~). If it is a 1 then it sends 0 left and 1 right, where the +s redirect them to join the flow to the output. If the first bit is 0 instead, the 0 goes right and the 1 goes left, and the + redirect them away from the output. All in all, this has the behaviour of stripping a leading zero if the input has one, otherwise prepending a zero if it doesn't, which means inverting the sign of the input.

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

Keg, 2 bytes

Try it online! !enilno ti yrT

Real simple. When run in normal direction, it simply prints the input. When run in reverse direction (±#), it negates the input.

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

TI-BASIC, 3 bytes

Ans-0

Very simple and works the same way as the top J answer.

Input is an integer in Ans.

Examples:

4:Ans-0
              4
4:0-Ans
             -4
\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog Unicode), 3 bytes

-/⊢
-/ ⍝ Summation with alternating sign
  ⊢ ⍝ Identity function

Reversed

⊢/-
⊢/ ⍝ Reduce (fold) with the Right operator
  - ⍝ Negate

Mirrored

⊣\-
⊣ ⍝ Left
 \ ⍝ Expand
  - ⍝ Negate

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Interestingly enough, -/⊢ -\⊢ -/⊣ -\⊣ all amount to identity functions, and ⊣\- ⊣/- ⊢\- ⊢/- all amount to negation functions... pretty cool :) \$\endgroup\$ Apr 1, 2021 at 0:44
0
\$\begingroup\$

Lua, 23 bytes

print(...)--)...-(tnirp

Try it online!

Comment abuse all over again.

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

Cascade, 6 bytes

# -
&#

Try it online!

and reversed:

#&
- #

Try it online!

I think this is the optimal answer for this question. In order to negate the input, we have to use the - operator, which means the code must be at least three wide so the left and the right operands aren't the same. It must then be two tall to have not loop infinitely. That means at least one row must be three wide, and the other has to be two wide, since if it was one wide, it must be the redirect to the #, which would have to be directly under it. Feel free to prove me wrong though.

Explanation:

For the initial program, we only execute # (integer output) on & (integer input). Basically, the only instructions that matter are:

#
&

For the reversed program, the # on the second line leads to - (left minus right). This wraps around the program, with the right being the &, and the left as an empty space, which is zero. This results in 0 - input, which is the negated input.

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

Phooey, 3 bytes

+.-

Try it online! (Forwards)

Try it online! (Backwards)

This assumes an empty stack and an empty tape.

Explanation:

Each instruction in Phooey has a modifier:

  • No modifier defaults to either the stack or the tape, depending on the instruction.
  • A number (with a preceding _ for negative) uses an immediate value
  • A . applies to an integer from stdin
  • A : applies to a char from stdin
  • A ! applies to the stack (for instructions where it isn't the default)

So, when read forwards, it is this:

+.-
+.  # Add to the tape
  - # pop and subtract (does nothing because stack is empty)

And backwards, it is the same for -.


Phooey, universal, 5 bytes

;0@.&

This version works regardless of the stack and tape state, and also preserves the stack contents.

Forwards:

;0@.&
;0    # tape = 0 - tape (don't care)
  @.  # read input and push to stack
    & # pop from the stack to the tape

Try it online! (Forwards)

Backwards:

&.@0;
&.    # read int and store to tape
  @0  # push 0 to the stack
    ; # pop, tape is 0 (stack) - tape

Try it online! (Backwards)

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

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.