I reverse the source code, you negate the output!

43

6

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.

HighlyRadioactive

Posted 2019-09-17T13:38:31.320

Reputation: 1 585

6If 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) – Jonathan Allan – 2019-09-17T21:23:14.303

@TwilightSparkle Does "have an unused, empty input" mean we can rely on input functions immediately reaching EOF? – Borka223 – 2019-09-18T12:56:37.767

1@Borka223 No, you may not. – HighlyRadioactive – 2019-09-18T13:44:59.517

@JoKing Copying. I will remove that. – HighlyRadioactive – 2019-09-19T04:16:45.513

Answers

44

05AB1E, 2 bytes

(1

Try it online!

!enilno ti yrT

(       # negate nothing
 1      # push 1 (and implictly output it)
1       # push 1
 (      # negate it (and implictly output it)

Grimmy

Posted 2019-09-17T13:38:31.320

Reputation: 12 521

10

+1 for "!enilno ti yrT"..

– Nat – 2019-09-19T09:58:14.270

28

JavaScript (V8), 19 bytes

print(1)//)1-(tnirp

Try it online!


almost identical to...

C# (Visual C# Interactive Compiler), 19 bytes

Print(1)//)1-(tnirP

Try it online!

(thanks to @someone for pointing it out)


still pretty much the same in...

Lua, 19 bytes

print(1)--)1-(tnirp

Try it online!


but shorter in...

Python 2, 15 bytes

print 1#1-tnirp

Try it online!


Even shorter in PHP, because it has this magic printing tool: <?= ...

PHP, 12 bytes

<?=1;#;1-=?<

Try it online!


Even shorterer in Ruby, because you can inspect rather than print

Ruby, 8 bytes

p 1#1- p

Try it online!

Arnauld

Posted 2019-09-17T13:38:31.320

Reputation: 111 334

Also C# Interactive, Print(1)//)-1(tnirP. (Try it online!)

– my pronoun is monicareinstate – 2019-09-17T14:04:19.800

For C#, the shortest program is likely also the trivial one: class A{static void Main(){System.Console.Write(1);}}//}};)1-(etirW.elosnoC.metsyS{)(niaM diov citats{A ssalc – LegionMammal978 – 2019-09-18T00:38:58.913

Not a programming language, but you can do 1<!--!<1- (9 bytes) with HTML, which will be -1<!--!<1 when reversed. It does exactly the same as your answer. – Ismael Miguel – 2019-09-18T10:08:02.577

Mostly the same in Lua: print(1)--)1-(tnirp – val says Reinstate Monica – 2019-09-18T19:10:45.760

@val This is a community wiki answer, so feel free to edit. – Arnauld – 2019-09-18T19:13:40.363

It would be nice to find answers for those languages, without any comment. – Eric Duminil – 2019-09-18T19:17:11.033

1

@EricDuminil: https://codegolf.stackexchange.com/a/193169/81663

– Night2 – 2019-09-19T11:33:10.893

28

PowerShell,  4  3 bytes

1-0

Try it online! or !enilno ti yrT

Golfed a byte by using arithmetic instead of the number-comment-number format.


This is apparently also the same as jshell (per Sam), and jq (per manatwork -- 1-0 and 0-1).

AdmBorkBork

Posted 2019-09-17T13:38:31.320

Reputation: 41 581

The same in jshell – Sam – 2019-09-19T10:59:14.560

The same in jq: 1-0 0-1.

– manatwork – 2019-09-19T11:55:54.157

12

///, 4 bytes

9/9-

Outputs 9.

Try it online!

Reversed:

-9/9

Outputs -9.

Try it online!

Everything before the / is printed, while the rest is ignored (not really used slashes much so I don't know exactly what happens, but it doesn't output anything).

Stephen

Posted 2019-09-17T13:38:31.320

Reputation: 12 293

2+1 for using Slashes. The / begins the pattern-reading process, and thus, the characters after it is readed into the pattern, not outputted. – HighlyRadioactive – 2019-09-17T14:09:17.500

2I came up with /\-//1- and thought I was clever. :D – Tanner Swett – 2019-09-19T00:44:38.413

10

Klein 011, 5 bytes

1-
@/

Try it online!

Reversed

/@
-1

Try it online!

These take advantage of Klein's unique topology, specifically the real projective plane. (Although individually each answer only needs a Klein bottle).

Post Rock Garf Hunter

Posted 2019-09-17T13:38:31.320

Reputation: 55 382

10

Whitespace, 21 bytes

S S S T N
T   N
S T N
N
N
T   S N
T   N
T   T   S S 

Letters S (space), T (tab), and N (new-line) added as highlighting only.

Outputs 1/-1.

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:

SSSTN  # Push 1 to the stack
TNST   # Pop and print the top of the stack as number
NNN    # Exit the program, making everything after it no-ops

The reverse program will:

SSTTN  # Push -1 to the stack
TNST   # Pop and print the top of the stack as number
NNN    # Exit the program, making everything after it no-ops

Small additional explanation of pushing a number:

  • First S: Enable Stack Manipulation
  • Second S: Push a number to the stack
  • S or T: Positive/negative respectively
  • Some S/T followed by a trailing N: number in binary, where S=0 and T=1

I.e. SSTTSTSN pushes -10.

Kevin Cruijssen

Posted 2019-09-17T13:38:31.320

Reputation: 67 575

7

T-SQL, 16 bytes

--Forwards:
PRINT 4--4-TNIRP

--Backwards:
PRINT-4--4 TNIRP

Picked 4 because 1 is overused :)

BradC

Posted 2019-09-17T13:38:31.320

Reputation: 6 099

1

If you are curious, an answer for MySQL would be select 1#1-tceles (17 bytes). You can check the result on http://sqlfiddle.com/#%219/9eecb/107825.

– Ismael Miguel – 2019-09-18T10:17:20.213

7

Japt, 2 bytes

Any single digit integer >0 can be used in place of the 2 as can A-G, H, I, J or L (10-16, 32, 64, -1 & 100, respectively).

n2

Test it | Reversed

The n method when applied to an integer, subtracts that integer from the argument passed to it, which defaults to 0. When run forwards, the n method is being run on the implicit first input, which also defaults to 0.

Alternatively, the g method could be used instead of n, which gives the sign of the result of subtracting its argument from the integer it's applied to.

Shaggy

Posted 2019-09-17T13:38:31.320

Reputation: 24 623

7

Haskell without comments, 41 bytes

Forwards prints 1 + newline:

main=print$!1
niam=main
"1-"!$rtStup=niam

Try it online!

Reversed prints -1 with no newline (which could be added at a cost of 2 bytes):

main=putStr$!"-1"
niam=main
1!$tnirp=niam

Try it online!

  • The first line of each program prints the number.
    • For -1 string output is used to avoid parentheses.
    • Using $! (strict application) instead of a space allows the reversed line to be a valid definition of the operator !$ (just $ wouldn't do since the redefinition would break the use).
  • The middle line ensures that niam is defined for the last line.
  • The last line is a definition of an operator !$, which is not used but needs to parse and typecheck correctly.

Ørjan Johansen

Posted 2019-09-17T13:38:31.320

Reputation: 6 914

7

Nat

Posted 2019-09-17T13:38:31.320

Reputation: 499

1This also works in TI-Basic – pizzapants184 – 2019-09-19T20:30:09.880

7

PHP, 15 13 bytes

A PHP version without comment abuse. ohce is an undefined constant, so it will be equal to string value of its name. As a result, this will try to print +1-'ohce' or -1+'ohce' when reversed. Since 'ohce' is a non-numeric value, 0 will be used instead in the arithmetic operations and only 1 or -1 will be printed.

;echo+1-ohce;

Try it online!

‮Try it online!

Night2

Posted 2019-09-17T13:38:31.320

Reputation: 5 484

That's clever. Nice! – AdmBorkBork – 2019-09-19T13:14:41.770

1Man, it's sad that I've arrived to the same answer as you, 3 hours late :( Have my upvote. – Ismael Miguel – 2019-09-19T14:58:41.670

6

Jelly, 2 bytes

NC

Try it online!

Negative, results in 0, then Complement, results in 1-0 = 1.


CN

Try it online! Complement, results in 1-0 = 1. Negative, results in -1.

AdmBorkBork

Posted 2019-09-17T13:38:31.320

Reputation: 41 581

6

K (ngn/k), 3 bytes

2-1

Try it online!

am i missing something? seems obvious for REPL languages

scrawl

Posted 2019-09-17T13:38:31.320

Reputation: 1 079

6

Cubix, 7 6 5 bytes

@)O(@

Try it here
Reversed

Explanation

Cubified:

  @
) O ( @
  .

Unrolling the control flow, we execute )O(@, which increments, outputs, decrements, and exits.

Reversed and cubified:

  @
( O ) @
  .

Unrolling the control flow, we execute (O)@, which decrements, outputs, increments, and exits.

Previous version

@O(.)O@

Try it here
Reversed

Not as short, but aesthetically pleasing.

user48543

Posted 2019-09-17T13:38:31.320

Reputation:

nice, way to put it on a size 1 cube! – Giuseppe – 2019-09-17T19:53:50.570

3@)O(@ for 5 bytes and restoration of symmetry :) – MickyT – 2019-09-17T22:01:53.983

5

Runic Enchantments, 4 bytes

1@Z1

Try it online! Try it Reversed!

I couldn't find a way to re-use the 1 command, even at the expense of a byte or two.

1@ɩ also works, but is the same number of bytes.

Draco18s no longer trusts SE

Posted 2019-09-17T13:38:31.320

Reputation: 3 053

Why the input? The question said your program must take no input. – HighlyRadioactive – 2019-09-17T13:51:46.627

@TwilightSparkle I misread and already fixed it – Draco18s no longer trusts SE – 2019-09-17T13:52:52.723

Yeah, I see. Clever. – HighlyRadioactive – 2019-09-17T13:54:06.713

5

APL (Dyalog Unicode),  4  3 bytes

1-0

Try it online!

Trivial answer. Prints 1 when run and ¯1 when run reversed.

J. Sallé

Posted 2019-09-17T13:38:31.320

Reputation: 3 233

5

Stack Cats -mn, 4 bytes

:-:_

Try it online! In the footer I've included all other 4-byte solutions. (Stack Cats ignores everything after the first linefeed.)

Try the reverse!

Explanation

The -n flag turns on numeric output (and input, but we don't have any), and the -m flag is normally just a golfing convenience which lets you avoid the redundant part of the source code. This is because every Stack Cats program needs to have mirror symmetry. With the -m flag you only give it the first half (plus the central character). So the actual program here is:

:-:_:-:

As you can see in the first TIO link, there's a ton of 4-byte solutions, but I picked this one for its simplicity. Stack Cats is stack-based, and this program only uses the initial stack. Since we don't have any input, it contains a single -1 (an EOF marker) on top of an infinite well of zeros. The three commands in the program have the following meaning:

:   Swap the top two stack elements.
-   Negate the top stack element (i.e. multiply by -1).
_   Pop a. Peek b. Push b-a.

So here is how the program modifies the stack (states and commands are staggered to indicate how each command changes the stack from one state to the next):

   :   -   :   _   :   -   :

-1   0   0  -1   1   0   0   1
 0  -1  -1   0   0   1   1   0
 0   0   0   0   0   0   0   0
 …   …   …   …   …   …   …   …

As it turns out, the only command that really does anything here is _ which turns our EOF marker into a 1. Output at the end of the program is implicit, and the EOF marker is optional, so this just prints out the 1 we get.

Now if we reverse the source code, due to the implicit mirroring, the actual program becomes:

_:-:-:_

This does something very different:

   _   :   -   :   -   :   _

-1   1   0   0   1  -1   0  -1
 0   0   1   1   0   0  -1  -1
 0   0   0   0   0   0   0   0
 …   …   …   …   …   …   …   …

This time the bottom of the stack is still a -1 so it does act as the EOF marker and only the -1 on top of it gets printed.

...

Now with all of that said, since Stack Cats has such a unique relationship with reversing code, I feel that using -m is a little cheating. It's normally only meant to save bytes by omitting the redundant part of the source code, but here it actually makes the challenge a lot easier and even the full program shorter. This is because reversing a full program will only change the program if it contains any of <>[], which also means that the program ends up making use of multiple stacks (Stack Cats actually has a tape of stacks, where all but the initial one are only filled with zeros to begin with). Furthermore, reversing it then just swaps the <> and [] pairs, which still makes the execution symmetric. The only way to break that symmetry is to use I which does -] or -[ or nothing depending on the sign of the top of the stack. So...


Stack Cats -n, 11 bytes

*|]I*:*I[|*

Try it online! The footer again includes all other alternatives at the same byte count. Some of those output 1/-1 and some output 2/-2 as indicated after each program. I picked this one to explain kinda randomly as one of the ones that output 2.

Try the reverse!

Explanation

As I said, this one's a bit longer. Even if we did use the -m notation for this, it would weigh in at 6 bytes instead of the above 4.

The commands in use this time:

*   Toggle the least significant bit of the top of the stack.
|   Reverse the longest non-zero of prefix on this stack.
[]  Move one stack to the left/right and take the top of the current stack with you.
I   If the top of the stack is positive, -], if it's negative, -[, otherwise do nothing.
:   Swap the top two stack elements.

The first program only uses two stacks. That's a bit messy to do in ASCII art, but I'll try my best. The square brackets indicate which stack the tape head is on, and I'll put the commands between each pair of stack states.

  [-1]
…   0   0   …
    0   0
    …   …

      *

  [-2]
…   0   0   …
    0   0
    …   …

      | (does nothing)
      ]

      [-2]
…   0   0   …
    0   0
    …   …

      I

   [2]
…   0   0   …
    0   0
    …   …

      *

   [3]
…   0   0   …
    0   0
    …   …

      :

   [0]
…   3   0   …
    0   0
    …   …

      *

   [1]
…   3   0   …
    0   0
    …   …

      I

      [-1]
…   3   0   …
    0   0
    …   …

      [

  [-1]
…   3   0   …
    0   0
    …   …

      |

  [ 3]
…  -1   0   …
    0   0
    …   …

      *

  [ 2]
…  -1   0   …
    0   0
    …   …

Now the -1 acts as an EOF marker and the 2 gets printed.

The other program is the same until the [. It's still virtually the same all the way until the second I. We'll technically be on a different stack, but without values on them, they're all indistinguishable. But then the difference between I[ and I] ends up mattering:

    *|[I*:*I

      [-1]
…   3   0   0   …
    0   0   0
    …   …   …

        ]

          [-1]
…   3   0   0   …
    0   0   0
    …   …   …

        | (does nothing)
        *

          [-2]
…   3   0   0   …
    0   0   0
    …   …   …

And this time, we don't have an EOF marker, but the program still outputs the -2.

Martin Ender

Posted 2019-09-17T13:38:31.320

Reputation: 184 808

4

CJam, 3 bytes

W;1

Try it online!

How they work

Normal version:

W    e# Push -1
;    e# Delete
1    e# Push 1
     e# Implicit display

Reverse version: you get the idea.

Luis Mendo

Posted 2019-09-17T13:38:31.320

Reputation: 87 464

4

Zsh, 12 bytes

<<<2 # 2-<<<

Try it online!

Basic forward, comment, reverse method.


If I/O is less restrictive, then a more interesting 11 byte solution is possible thanks to Zsh supporting negative return codes:

return -127

Reversed, 721- nruter exits with code 127 (command not found). exit -127 cannot be used, it would be cast to a u8. Try it online!

GammaFunction

Posted 2019-09-17T13:38:31.320

Reputation: 2 838

4

MATL, 3 bytes

Nqv

Try it online!

How they work

Normal:

N   % Push number of elements in the stack: 0
q   % Subtract 1: gives -1
v   % Concatenate stack contents vertically: leaves -1 as is
    % Implicit display stack contents

Reversed:

v   % Concatenate stack contents vertically: gives the empty array, []
q   % Subtract 1: leaves [] as is
N   % Push number of elements in the stack: 1
    % Implicit display. [] is not displayed

Luis Mendo

Posted 2019-09-17T13:38:31.320

Reputation: 87 464

4

Wolfram Language (Mathematica), 21 18 bytes

Print@1;tnirP//1-0

Try it online!

-3 thanks to Martin Ender

attinat

Posted 2019-09-17T13:38:31.320

Reputation: 3 495

4

Hexagony, 5 bytes

1!@!(

Try it online!

Any valid program must:

  • Have a termination command (@ or :). The latter is only different for the former when there's a memory pointer movement command. Also this command must not be at the first or the last byte.
  • Have an output command. (!, ; is also possible but would probably take more bytes)
  • Have a memory manipulation command.

Therefore a 2-byte program is obviously impossible. A 3-byte program is impossible because the second byte must be the termination command, and the first byte must not be a mirror/IP manipulation command, therefore only 1 byte can be executed.

I think a 4-byte program is not possible. Such a program must have the form a@bc with hexagonal grid

 Forward:       | Backward:
                | 
  c b           |   a @
 @ a .          |  b c .
  . .           |   . .

Therefore a must be a IP redirection command. However it's impossible to generate both positive and negative number with only 1 memory manipulation command.

user202729

Posted 2019-09-17T13:38:31.320

Reputation: 14 620

+1 for proof of optimality in an otherwise trivial answer – Jo King – 2019-09-23T00:48:23.350

3

Haskell, 28 bytes

main=print 1--)1-(tnirp=niam

Try it online!

Try it enilno!

Sacchan

Posted 2019-09-17T13:38:31.320

Reputation: 621

3

Triangular, 4 bytes

i%%d

Try it online!

Outputs 1.

Ungolfed:

   i
  % %
 d

Reversed:

d%%i

Try it online!

Outputs -1.

Reinstate Monica

Posted 2019-09-17T13:38:31.320

Reputation: 1 382

3

Perl 5 (-p), 12 bytes

\$--{}}{++$\

Try it online!

!enilno ti yrT

The }{ pseudo-operator really comes in handy.


Perl 5 (-M5.010), 9 bytes

Provided by Nahuel Fouilleul in a comment

say+1-yas

Try it online!

!enilno ti yrT

Grimmy

Posted 2019-09-17T13:38:31.320

Reputation: 12 521

2should be say 1#1-yas – Nahuel Fouilleul – 2019-09-17T15:24:43.130

@NahuelFouilleul yas indeed – Grimmy – 2019-09-17T15:27:51.910

19 bytes : say+1-yas and say-1+yas – Nahuel Fouilleul – 2019-09-18T04:56:41.923

3

Java 5 or 6, 127 67 bytes

enum A{A;{System.out.print(9);}}//}};)9-(tnirp.tuo.metsyS{;A{A mune

Outputs 9/-9.

No online compiler, because Java 5 or 6 isn't available anywhere.

You can however try this 127 bytes Java 8 equivalent:
Try it online or try it online reversed.

Explanation:

enum A{                              // Create an enum
 A;                                  //  With a mandatory value
 {                                   //  And in a separate instance code-block:
  System.out.print(9);}}             //   Print 9 to STDOUT
//}};)9-(tnirp.tuo.metsyS{;A{A mune  // Comment and thus a no-op

Java 5 and 6 had a bug allowing you to create a code block inside an enum to do something, despite missing a program's mandatory main-method. This will result in an error:

java.lang.NoSuchMethodError: main
Exception in thread "main"

But will still output what we'd want to STDOUT first, so we can ignore that.

Kevin Cruijssen

Posted 2019-09-17T13:38:31.320

Reputation: 67 575

3

Golang, 109 bytes

package main;import "fmt";func main(){fmt.Println(1)}//})1(nltnirP.tmf{)(niam cnuf;"tmf" tropmi;niam egakcap

And its reverse:

package main;import "fmt";func main(){fmt.Println(-1)}//})1(nltnirP.tmf{)(niam cnuf;"tmf" tropmi;niam egakcap

Try it online!

TheMrMittens

Posted 2019-09-17T13:38:31.320

Reputation: 31

2I don't know Go, but it seems you can remove a few bytes. The Println can be Print, and the import "fmt"; doesn't need the space: import"fmt";. :) – Kevin Cruijssen – 2019-09-19T08:52:41.483

3

\$\Large{\mathbf{\TeX} \left(\text{MathJax}\right)},~4\,\text{bytes}\$

1%1-

$$ \begin{array}{|r|c|c|} \hline & \textbf{Code} %& \begin{array}{c}\boxed{\textbf{Boxed}} \\ \textbf{Result} \end{array} & \textbf{Result} \\\hline \textbf{Forward:} & \texttt{1%1-} & %\boxed{ 1%1- %} \\\hline \textbf{Backward:} & \texttt{-1%1} & %\boxed{ -1%1 %} \\\hline \end{array} $$

Nat

Posted 2019-09-17T13:38:31.320

Reputation: 499

3

Retina, 6 bytes

-`<
-

Prints 1.

Try it online!



-
<`-

Prints -1.

Try it online!

Explanation: 1

-`<
-

This... does nothing. Due to the `, this is a substitution from < to - (with configuration -, which does nothing), but the input is empty, so the output is empty as well.


And this second stage matches the empty regex against the empty input and counts the number of matches, which is exactly 1. Output is implicit.

Explanation: -1


-

This time we replace the empty regex with -. This does indeed turn the empty input into a single -.

<`-

Here, the configuration actually does something: < prints the stage's input before executing the stage, so we print the -. Then - counts the hyphens in the stage's input which is again 1. Due to the implicit output, this prints a 1 after the -, giving us -1 as required.

Martin Ender

Posted 2019-09-17T13:38:31.320

Reputation: 184 808

3

brainfuck, 156 bytes

+++++++++++++++++++++++++++++++++++++++++++++.+++++++++++++++++++++++++++++++++++++++++++++++++<+++++++++++++++++++++++++++++++++++++++++++++++++.++++++++++

Try it online! / Forward/backward verifier in Bash

Prints -1 forward and \n1 backwards.

Despite being almost trivial, I believe this is the optimal solution for this particular fixed output.

Proof:

  • The program cannot have [ or ].

    Therefore the program must have the form <A> . <B> . <C>.

  • Each , can be replaced with a sufficient number of < without increasing the number of + or -.

  • Each + is only useful in either the forward or the backward program, never both.

    Proof: + in part A is obviously only useful in the forward program, and + in part C is obviously only useful in the backward program.

    Denote shift(P) = number of < in P - number of > in P. Consider program <B> = <D> + <E>, the + in the middle is useful in the forward program \$\iff\$ shift(E) = 0, similarly it's useful in the backward program \$\iff\$ shift(D) = 0. However if shift(D) = shift(E) = 0 then the program B executed either forward or backward would add a fixed value to the current cell before printing the second time, which can't be the case because ord('1') - ord('\n') != ord('1') - ord('-').

Therefore the program needs at least ord('-')+ord('1')+ord('\n')+ord('1') = 153 +s, 2 .s, and at least a < > or , because shift(B) != 0.

user202729

Posted 2019-09-17T13:38:31.320

Reputation: 14 620

2

MarioLANG, 5 bytes

+
:
-

Try it online!

Reversed:

-
:
+

Try it online!

Reinstate Monica

Posted 2019-09-17T13:38:31.320

Reputation: 1 382

2

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.

Khuldraeseth na'Barya

Posted 2019-09-17T13:38:31.320

Reputation: 2 608

2

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.

ouflak

Posted 2019-09-17T13:38:31.320

Reputation: 925

2

JavaScript, 31 bytes

The obligatory comment abuse for JavaScript!

console.log(1)//)1-(gol.elosnoc

and reversed:

console.log(-1)//)1(gol.elosnoc

geisterfurz007

Posted 2019-09-17T13:38:31.320

Reputation: 121

2

><>, 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

Borka223

Posted 2019-09-17T13:38:31.320

Reputation: 311

2

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

reffu

Posted 2019-09-17T13:38:31.320

Reputation: 1 361

2

PHP, 10 bytes.

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

<?=1#1-=?<

Martijn

Posted 2019-09-17T13:38:31.320

Reputation: 713

1I haven't tested this, but I suspect the last > should be <. – Ørjan Johansen – 2019-09-21T05:38:28.673

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: https://codegolf.stackexchange.com/a/192982/81663

– Night2 – 2019-09-21T11:58:02.837

1

Python 3, 18 bytes

print(1)#)1-(tnirp

Try it online!

Jitse

Posted 2019-09-17T13:38:31.320

Reputation: 3 566

1

Ruby, 8 7 bytes

p 1#0~p

Try it online!

Thanks Arnauld for chipping off 1 byte and making it less dull.

G B

Posted 2019-09-17T13:38:31.320

Reputation: 11 099

1

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)

Neil

Posted 2019-09-17T13:38:31.320

Reputation: 95 035

1

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.

Unrelated String

Posted 2019-09-17T13:38:31.320

Reputation: 5 300

1

Stax, 5 bytes

1PsPU

Not going to win any awards, but this one is refreshingly straightforward. Depends on input being empty.

Joshua

Posted 2019-09-17T13:38:31.320

Reputation: 3 043

1

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!

James

Posted 2019-09-17T13:38:31.320

Reputation: 54 537

1

Keg, 5 bytes

1#-10

Explanation:

1#    Push 1
 #-10 Comment

Reversed explanation:

01-#  Push -1
   #1 Comment

user85052

Posted 2019-09-17T13:38:31.320

Reputation:

1

Befunge-93 (FBBI), 7 6 bytes

1-.@.1

-1 byte thanks to @squid.

Try it online!

Krzysztof Szewczyk

Posted 2019-09-17T13:38:31.320

Reputation: 3 819

1

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.

bigyihsuan

Posted 2019-09-17T13:38:31.320

Reputation: 1 483

0

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)

Kevin Cruijssen

Posted 2019-09-17T13:38:31.320

Reputation: 67 575

Why does MathGolf have a builtin for -3? – HighlyRadioactive – 2019-09-17T14:34:14.510

1@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. ;) – Kevin Cruijssen – 2019-09-17T14:39:51.563

Why not a built in for the Ultimate Answer(42), like AlephAlpha's esolang, Samau? – HighlyRadioactive – 2019-09-18T04:29:35.510

@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.

– Kevin Cruijssen – 2019-09-18T07:10:06.370

0

C (clang), 39 bytes

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

Try it online!

Corrected as @AdamBorkBork error spotting.

AZTECCO

Posted 2019-09-17T13:38:31.320

Reputation: 2 441

1Darn! You beat me by... 4 hours. – S.S. Anne – 2019-09-17T19:04:05.743

0

Bash, 16 bytes

echo -1 # 1 ohce

Try it online!

S.S. Anne

Posted 2019-09-17T13:38:31.320

Reputation: 1 161

0

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

Giuseppe

Posted 2019-09-17T13:38:31.320

Reputation: 21 077

0

Python REPL, 4 bytes

1-0

Only works in the Python REPL environment.

Shaved one byte based on suggestion from Ilmari Karonen.

bob

Posted 2019-09-17T13:38:31.320

Reputation: 109

Welcome to the site! As this only works in Python REPL, I've edited the language to reflect that. – caird coinheringaahing – 2019-09-18T19:30:09.110

Thanks, I appreciate that! – bob – 2019-09-18T19:48:19.333

Wouldn't 1-0 be one byte shorter? – Ilmari Karonen – 2019-09-18T21:49:12.993

It would, yes. I'll update it. – bob – 2019-09-19T12:36:36.967

0

Putt, 3 2 bytes

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

Reverse:

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

ajmwagar

Posted 2019-09-17T13:38:31.320

Reputation: 11

0

dc, 4 bytes

1p1_

prints 1

in reverse,

_1p1

prints -1

Radovan Garabík

Posted 2019-09-17T13:38:31.320

Reputation: 437

0

ink, 6 bytes

1//1-\

Try it online!

Reversed

\-1//1

Try it online!

Sara J

Posted 2019-09-17T13:38:31.320

Reputation: 2 576

0

GolfScript, 4 bytes

Simply abusing comments to remove a number.

1#1-

TIO

Reversed:

-1#1

TIO

user85052

Posted 2019-09-17T13:38:31.320

Reputation: