-11
\$\begingroup\$

You have to write a program, if its source code is reversed, then it will error.

For example, if your source is ABC, then reversing it and writing CBA should crash/throw an error. And ABC itself should be a vaild program (I mean your primary source should be a valid program). Output of the main source code can be empty, but your ABC code I mean primary source should not output anything to standard error.

Also the minimum length of a solution is 2 bytes.

Standard loopholes apply, shortest code wins.

\$\endgroup\$
8
  • \$\begingroup\$ @Wasif Would be too similar to challenges in this vein: codegolf.stackexchange.com/questions/192979/… \$\endgroup\$ – Razetime Feb 27 at 7:06
  • 5
    \$\begingroup\$ Basically any programming language (with the exception of Stack Cats) will error when its code is reversed. \$\endgroup\$ – Razetime Feb 27 at 7:10
  • \$\begingroup\$ @Razetime another idea is too make the main source error and reversed one valid? (That might be more interesting) \$\endgroup\$ – Wasif Feb 27 at 7:10
  • \$\begingroup\$ The question and its idea have already been posted. Changing that would make it a different question, and require all the answers to be deleted (also, that is effectively the same challenge) \$\endgroup\$ – Razetime Feb 27 at 7:13
  • \$\begingroup\$ @Razetime another possibility to make this a little interesting is to restrict byte limit for this challenge (To 3-4 bytes or somehow like this), it would not require us to delete our answers, because everything is within 2-3 bytes, so possible solutions will be reduced \$\endgroup\$ – Wasif Feb 27 at 7:24

19 Answers 19

4
\$\begingroup\$

R, 2 bytes

NA

Try it online!

Outputs NA, which corresponds to "Not Available", to represent missing values.

On the other hand, AN errors, as it is literally not available.

A bunch of other examples:

1i
1L
ar
as
by
de
df
dt
gc
gl
Im
is
lh
lm
ls
pf
pi
pt
qf
qr
qt
Re
rf
rt
sd
ts
vi
{}
.C
\$\endgroup\$
4
\$\begingroup\$

Malbolge, 2 bytes

Errors via raising a SIGILL (illegal instruction).

DP

Try it online!

Try it reversed!

Explanation

Forward:

C [C] (C+[C])%94 Action
0 D (ord 68) 68 No operation.
1 P (ord 80) 81 Stop execution of current program.

Reversed:

C [C] (C+[C])%94 Action
0 P (ord 80) 80 Illegal instruction (stops program)
1 D (ord 68) 69 Illegal instruction (not executed)
\$\endgroup\$
3
\$\begingroup\$

Python 2, 2 bytes

And probably 1 million different languages too

+1

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Python 2 or 3, 3 bytes

0/1

Try it online!

Try it online (Reversed)!

0/1 is a valid mathematical expression and will result 0 (will show nothing on TIO interpreter) 1/0 (reversed) causes a divide by zero error.

Polygots in a "loooottttttttttt" of languages

\$\endgroup\$
5
  • \$\begingroup\$ Polyglot in about 9 million languages \$\endgroup\$ – pxeger Feb 27 at 7:00
  • \$\begingroup\$ @pxeger I think so, answers are too broad! \$\endgroup\$ – Wasif Feb 27 at 7:01
  • \$\begingroup\$ You can do better: [] is a valid line of code too. \$\endgroup\$ – Don Thousand Feb 27 at 19:22
  • \$\begingroup\$ Two bytes: (), {}, [], 10, +0, ~0, -0, #!, 0;, and many, many more. \$\endgroup\$ – Makonede Mar 5 at 22:13
  • \$\begingroup\$ Another option: 0\ (without the space) \$\endgroup\$ – Makonede Mar 6 at 19:57
3
\$\begingroup\$

Jelly, 2 bytes

D€

Try it online! or !enilno ti yrT

How it works

D€ - Main link. Takes no arguments
   - Implicitly set the argument to 0 as the chain doesn't begin with a nilad
 € - Generate the range from 1 to 0 ([]) and over each element:
D  -   Get its digits
     Output the empty list, which outputs nothing due to Jelly's output rules

skrow ti woH

€D - Main link. Takes no arguments
€  - € is a quick, which needs a preceding atom upon which to act
     Without a preceding atom, parsing fails and the program errors
 D - D is ignored as the program has errored

Alternative programs

There are far too many 2-byte programs that also work to fully list them. Instead, here's how to make your own:

  1. Open Jelly on Try it online!
  2. Open the Quicks page
  3. Open the Atoms page
  4. Choose any single character atom. Enter it into the TIO Code field (Copy-Paste works best)
  5. Choose any single character quick. Enter it after the atom
  6. Press Run.
  7. If this errors, clear the characters and go to step 4
  8. Voila, you have your answer
\$\endgroup\$
2
\$\begingroup\$

Python 3, 2 bytes

G B already posted one of those, but I wanted to show some more 2-byters:

+0
0,
~0
#\
id
0j
0;
{}
0 

Try it all online!

Here is a more complete list.

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

JavaScript, 2 bytes

[]

Errors with ][

\$\endgroup\$
1
  • \$\begingroup\$ @Wasif I know, this challenge is a little broad \$\endgroup\$ – ophact Feb 27 at 7:50
1
\$\begingroup\$

Bash, 2 bytes

wc

Try it online!

Try it online (Reversed)!

Don't think this one has a lot of "polygot"s

other 2-byters sh,ps,du,ls (if sl is not installed)

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

Windows Batch, 2 bytes

:a

This program is an empty label, if reversed then it would search for a drive named a:, but floppy drives are no longer available!!!

\$\endgroup\$
1
  • \$\begingroup\$ My Windows VM has a virtual floppy drive, does that count? \$\endgroup\$ – Neil Feb 27 at 11:34
1
\$\begingroup\$

Vyxal, 2 bytes

i1

Try it Online! or Try it Reversed!

Explained

Before we begin, i takes two arguments (we'll call them a and b) and returns a[b] (indexing). 1 simply pushes 1 to the stack.

The program i1 is:

rhs, lhs = pop(stack, 2)
stack.append(lhs[rhs])
stack.append(1)

Because the stack is empty, implicit input is taken for lhs and rhs. And because there is no input, 0 is returned each time input is needed. Thus, i gives "0"[0] and then pushes 1.

The program 1i is:

stack.append(1)
rhs, lhs = pop(stack, 2)
stack.append(lhs[rhs])

Which attempts to push "0"[1], which is obviously out of the list. Vyxal doesn't have modular indexing (yet), so this errors.

\$\endgroup\$
3
  • \$\begingroup\$ You always seem to answer in Vyxal because your username is very similar \$\endgroup\$ – ophact Feb 27 at 7:49
  • 1
    \$\begingroup\$ Heh, funny about that. ;p \$\endgroup\$ – lyxal Feb 27 at 8:16
  • 1
    \$\begingroup\$ Whereas in R, 1i is valid, but i1 isn't. ;-) \$\endgroup\$ – Robin Ryder Feb 27 at 8:17
1
\$\begingroup\$

naz, 2 bytes

1o

Any valid 2-byter will throw the error missing number literal if reversed; I just picked this one because it actually outputs something (0).

Try it online! / Try it reversed!

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

MATLAB/Octave, 2 bytes

pi

When run forward, this yields an approximation of pi represented as a floating point number. When reversed, it tries to execute the function named ip which is not defined:

octave:1> ip
error: 'ip' undefined near line 1, column 1
\$\endgroup\$
1
\$\begingroup\$

Java, 42 bytes

interface M{static void main(String[]a){}}

This is the shortest valid full Java program. It does not output anything. Since all methods in an interface are implicitly public, we can save bytes by putting the main method inside an interface instead of a class.

Try it online!

The reversed code is clearly invalid syntax:

}}{)a][gnirtS(niam diov citats{M ecafretni

Try it online!

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

Lua (2 bytes)

Write the following in the command line

=1

Although the =<something> is supported for backward compatibility, is a deprecated feature in Lua. 1= triggers the following error:

stdin:1: unexpected symbol near '1'

in Lua 5.4.2.

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

x86-16 machine code, 2 bytes

Full program. Error type is an unhandled SIGSEGV.

Hexdump:

00000000: 63c3                                     c.

Listing:

63         DB 63      ; Raises an unhandled SIGSEGV
C3         RET        ; Not executed

Listing (reversed):

C3         RET        ; Return to caller
63         DB 63      ; Not executed
\$\endgroup\$
0
\$\begingroup\$

[BASIC], 2 bytes

?a

Try it online!

a?

The reverse will just generate a Syntax Error

Basically (pun totally intended) if you can't make a 2-3 byte program in almost any language that'll crash if reversed, something is wrong!

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

MATL, 2 bytes

lt

Try it at MATL Online

Explanation

This program pushes the literal 1 to the stack and duplicates it using t. When reversed, t tries to grab it's input argument implicitly and since no inputs are provided it errors out

MATL run-time error: The following Octave error refers to statement number 1: t
---
Unable to fetch user input
\$\endgroup\$
0
\$\begingroup\$

Brain-flak, 2

()

Errors on )(

PHP, 2

$=?> does nothing

<?=$ errors.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy