Terang-terangan rip-off dari rip-off . Pilih mereka itu!
Tugas Anda, jika Anda ingin menerimanya, adalah menulis program / fungsi yang mengeluarkan / mengembalikan input / argumen bilangannya. Bagian yang sulit adalah jika saya membalikkan kode sumber Anda, hasilnya harus bilangan bulat asli yang dinegasikan.
Contohnya
Katakanlah kode sumber Anda ABC
dan inputnya 4
. Jika saya menulis CBA
dan menjalankannya, hasilnya harus -4
.
Katakanlah kode sumber Anda ABC
dan inputnya -2
. Jika saya menulis CBA
dan menjalankannya, hasilnya harus 2
.
Masukan dari 0
dapat memberi 0
atau -0
, bagaimanapun, jika Anda benar-benar mendukung nol, -0
harus memberi 0
.
Jawaban:
PowerShell,
1814 bytesTry it online! !enilno ti yrT
First of the trivial comment-abuse answers
sumber
J, 3 bytes
Try it online!
-&0
is "argument minus 0"0&-
is "0 minus argument"sumber
][-
etc.JavaScript, 11 bytes
Try it Online! | Reversed
sumber
x86 machine code, 3 bytes
The above bytes of code define a function that is a no-op: it simply returns control to the caller. That function is followed by two garbage bytes that will not be executed, since they come after a return—they are in "no man's land". In assembler mnemonics:
Okay, so now some troll comes by and reverses the order of the bytes:
These bytes now define a function that takes an integer argument in the
EAX
register, negates it, and returns control to the caller. In assembler mnemonics:So…that was simple. :-)
Note that we can make the "negation" instruction be anything we want, since it is never executed in the "forward" orientation and only executed in the "reversed" orientation. Therefore, we can follow the same pattern to do arbitrarily more complicated stuff. For example, here we take an integer argument in a different register (say,
EDI
, to follow the System V calling convention commonly used on *nix systems), negate it, and return it in the conventionalEAX
register:↓ ↓
sumber
Jelly, 2 bytes
Try it online! and its reverse.
sumber
ḷN
, no need for the OR logic. :DNa
, words giveaN
aesthetic effect :)Haskell, 8 bytes
Anonymous identity function, turning into subtraction from
0
when reversed.Try it online!
Reversed:
Try it online!
sumber
Whitespace, 48 bytes
Letters
S
(space),T
(tab), andN
(new-line) added as highlighting only.Minor modification of my Whitespace answer for the I reverse the source code, you negate the output! challenge.
Try it online or try it online reversed (with raw spaces, tabs and new-lines only).
Explanation:
Utilizing the Exit Program builtin being a short palindrome
NNN
.The regular program will:
The reverse program will:
Small additional explanation of pushing a number:
S
: Enable Stack ManipulationS
: Push a number to the stackS
orT
: Positive/negative respectivelyS
/T
followed by a trailingN
: number in binary, whereS=0
andT=1
I.e.
SSTTSTSN
pushes-10
. For the0
we don't need an explicitS=0
, so simplySSSN
orSSTN
is enough.sumber
Perl 6 / Raku, 3 bytes
Try it online!
Creates a Whatever code block. Read in normally its standard block equivalent is
-> \x {x - 0}
, but in reverse it becomes-> \x {0 - x}
.sumber
C (clang), 23 bytes
Try it online!
sumber
Brain-Flak, 7 bytes
Try it online!
Reversed:
Try it online!
Note: Only works in interpreters that support comments (e.g. works in Rain-Flak, but not in BrainHack)
If we also swap opening/closing brackets instead of just reversing the bytes we can do this in 8 bytes without using comments:
Try it online!
Try it reversed!
sumber
#
starts a comment, so the parenthesis in the original version are ignored.Labyrinth / Hexagony, 6 bytes
Labyrinth:
Try it online! and its reverse.
Hexagony:
Try it online! and its reverse.
How?
sumber
Python 3, 22 bytes
Try it online!
A lambda which implements the identity function (or negation)
sumber
MarioLANG, 22 bytes
Try it online!
He just inputs and outputs the number before he falls to EOF
reversed:
Try it online!
He loops until the input value is 0 and the output value is -input, the he says the number.
sumber
R, 23 bytes
I decided to give it a go without the comment trick.
Forward
Try it online!
Reverse
Try it online!
In the forward version
+
is acting a binary operator, and-
is a unary operator.In the reverse the
+
becomes unary and the-
is binary. So scan function takes the arguments:file=""
which means stdin andwhat=0
, which are also defaults. So when the+
is unary the first argument is on the right, when it is binary the first argument is on the left.The
part of the code does nothing really useful, so in a sense my code is not really very much more valid than using the comment trick.
sumber
+
redefined to be used as both unary and binary. It took me a minute to understand how this was parsed… No other operator name would have done the job.Haskell, 12 bytes
Try it online! Reverse:
Try it online!
Not as short as Ørjan Johansen's answer, but without comments.
sumber
Perl 5 (
-p
),76 bytes-1 thanks to @primo
TIO
A comment doesn't change input
Negate the input
TIO
sumber
$_*=$#
TIO. Note that the#
must be the very last byte of the program, otherwise it will be interpreted as the variable$#
, rather than the last index of the array with name <empty>.$#
gives either an error (if # is not the last character) or nothing-p
or-n
. I suspect the boilerplate has something to do with it...-p/-n
adds a;
after the code. Which means that$#
is actually$#;
: the size of the array@;
. If the size of@;
changes, the result isn't correct anymore (TIO). Anyway, this is super clever, well done! :)perl -MO=Deparse -p <(echo -n '$_*=$#')
, because it seemsperl -MO=Deparse -pe '$_*=$#'
adds a newlineGaia, 2 bytes
Try it online!
Reversed:
sumber
Backhand,
65 bytesTry it online! Try it doubled!
Made a little complex due to the nature of the pointer in Backhand.
I don't think it's possible to get any shorterhaha, turns out I was wrong. This duplicates no instruction and reuses both the input, output and terminate commands between the two programs. Now I think it is optimal, since you need all of theIO-@
commands to work, and in a 4 byte program you can only execute two of those commands.Explanation:
The pointer in Backhand moves at three cells a tick and bounces off the boundaries of the cell, which means the general logic is overlapping. However you can manipulate this speed with the
v
and^
commands.The original program executes the instructions
IO-@
, which is input as number, output as number, subtract, terminate. Obviously the subtract is superfluous. In the code these are:The reversed program executes
v-I-vO-@
. Thev
reduces the pointer steps between ticks, and the-
subtracts from the bottom of the stack, which is implicitly zero. The extra-
commands do nothing. The program executes likesumber
Wolfram Language (Mathematica), 9 bytes
Try it online!
Forward: read
((1)&*0+#-0)&*1
=#&
Backward: read
((1)&*0-#+0)&*1
=-#&
sumber
R, 14 bytes
Try it online!
A full program that reads a number, or reads and negates a number. The reverse functionality is protected by an inline comment
sumber
Python 3,
2214 bytesTry it online!
Uses the
int
class's constructor and a built-in pseudo-private method.sumber
0.
would be interpreted as a number, which is followed by a symbol05AB1E, 2 bytes
Try it online!
Reversed
sumber
Befunge-98 (FBBI), 6 bytes
Try it online! Try it reversed!
sumber
APL (Dyalog Unicode),
133 bytesTry it online!
Trivial answer. Returns
arg
or¯arg
.Saved 10 bytes by not being dumb (thanks Adám).
Altered the resulting 3-byter to a more fitting function.
sumber
Turing Machine Language, 39 bytes
The Positive
The Negative
This one was a bit trickier than I thought, mostly because I had to get past my prejudices of having code that runs with 'compile' errors.
sumber
><>,
54 bytesuses stack initialisation with the
-v
option, put your input variable there.Try it online!
Or try the reversal
Explanation
sumber
Stack Cats
-mn
, 2 bytesTry 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
givesX-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 bytesTry 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|
orT
(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: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:sumber
Batch, 34 bytes
Echoes (
ECHO.
) the input (%1
). The rest of the first line technically redirectsSTDERR
to a file calledMER@
, but this isn't impactful.Second line is commented out (
REM...
).Reversed
Uses the arithmetic mode of the set command (
SET /a
) to subtract (-=
) the input (%1
) from an undefined variable (a
) which is equivalent to0 - input
. Again, the rest of the first line technically redirectsSTDERR
to a file calledMER@
, but this isn't impactful.Second line is commented out (
REM...
).sumber
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
sumber
Triangular,
87 bytesTry it online!
Ungolfed:
Reversed:
Try it online!
Previous Version (8 bytes):
sumber