20
\$\begingroup\$

Your task is to write a program, function, or snippet that will output one value if run forward, and another if run backward. The program must run without errors.

The output can be any two distinct values, of the same data type. For example, 1 and 3, true and false, or 't' and 'f'. In languages without a concept of data types, such as brainf*** or text, any distinct values are allowed.

This is a code golf challenge, shortest answer per language wins. Note that snippets are allowed (so something like 1-2 is valid, but don't expect any upvotes).

\$\endgroup\$

42 Answers 42

1
2
1
\$\begingroup\$

PHP, 2 bytes

12

Try it online!

Prints 12 ...

21

Try it online!

Prints 21 ...


Looking for non-trivial solution?

PHP, 11 bytes

<?=1;#;2=?<

Try it online!

Prints 1 ...

<?=2;#;1=?<

Try it online!

Prints 2 ...


Don't like comment abuse?

PHP, 13 bytes

;echo+1-ohce;

Try it online!

Prints 1 ...

;echo-1+ohce;

Try it online!

Prints -1 ...

Note: this one is an exact copy of my answer here: https://codegolf.stackexchange.com/a/193169

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ The definition of "reversed" is ambiguous in the question, and could be byte-reversed, as you have, line-reversed... or bit-reversed, which means you can get it down to one byte (eg J is binary 0100 1010, which reversed is 01010 0010, or R). I don't think you can get shorter than that, though, as I don't think sub-byte sizes are valid input to the parser. \$\endgroup\$ – Dewi Morgan Nov 9 '19 at 15:41
  • \$\begingroup\$ @DewiMorgan Reversed in terms of characters \$\endgroup\$ – Redwolf Programs Nov 10 '19 at 15:16
  • \$\begingroup\$ @RedwolfPrograms I get that it's what you meant, but since it was left ambiguous, it makes that aspect of the request fair game for golfing, too. All three could give interesting results, though I think the bitwise reverse would be the most trivial in many languages, and impossible in others, and the winner would likely be any language which allowed sub-byte inputs; while the command-wise reversal would for once give no advantage to golfing languages, if the number of operators is being counted, rather than number of characters. \$\endgroup\$ – Dewi Morgan Nov 11 '19 at 17:47
1
\$\begingroup\$

C (gcc), 21 bytes

puts("");//;)"0"(stup

Try it online!

!enilno ti yrT

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Runic Enchantments, 3 bytes

0@1

Try it online!

Prints 0 forwards and 1 backwards.

If no output is allowed, then @1 saves a byte.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Never realized you made this language...seems pretty cool! \$\endgroup\$ – Redwolf Programs Nov 10 '19 at 1:49
  • 1
    \$\begingroup\$ @RedwolfPrograms Yep! Its almost the only language Igolf in because of that. \$\endgroup\$ – Draco18s no longer trusts SE Nov 10 '19 at 16:56
1
\$\begingroup\$

Rebol, 2 bytes

#1

Prints #1

1#

Prints ##

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Whitespace, 20 bytes

S S S 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 0/-1.

Try it online or try it online reversed (with raw spaces, tabs and new-lines only).

Explanation:

Very minor modification of my answer here (outputting 0 is 1 byte shorter than 1), so I'm just gonna copy-paste the explanation:

Utilizing the Exit Program builtin being a short palindrome NNN (also note that without an exit, every Whitespace program would result in an error, making it invalid for this challenge).
The regular program will:

SSSN   # Push 0 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. Only the first three and trailing N are mandatory, so SSSN/SSTN will both push 0.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

SimpleTemplate, 10 bytes

This was actually hard to optimize, without going into the trivial answer.

{@echo"1"}

Outputs the string "1".

When reversing, you get the following:

}"1"ohce@{

Outputs the string }"1"ohce@{.

As stated in the question: "The output can be any two distinct values, of the same data type.", and both solutions output a distinct string.

You can test this on http://sandbox.onlinephpfunctions.com/code/ec4bf4b3efa93c63c6c5c9f1458330da0178b6cd
(On line 960, change between the $code and $reversed variables)



SimpleTemplate, 2 bytes

This one is extremelly trivial:

<any 2 different bytes>

Reversing them will output whatever is reversed.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Java, 14 bytes

()->1;//;0>-)(

Simple function that uses comments to exclude the other part. Outputs 1, and reversed outputs 0.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Ahead, 4 bytes

O@j1

Prints 0 forward and 1 backward.

Forward

O     pop stack and print number
 @    end
  j1  not reached

Backward

1     push 1
 j    jump next cell
   O  pop stack and print number
  @   end

Control flow is more interesting backward because I tried to reduce the amount of redundant characters.

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

GolfScript, 2 bytes

Simply prints 1 because the duplicate instruction duplicates the null string.

.1

Reversed

Outputs 11 because that is the value 1 duplicated.

1.

Try it online!

GolfScript, 4 bytes

Outputs 0 because 1 - 1 is 0.

1 1-

Reversed

Outputs -11 due to concatenation.

-1 1

Try it online!

GolfScript, 2 bytes (Am I allowed to use the null string?)

Outputs the null string, because the null string from the input minus 1 is still the null string.

1-

Reversed:

Pushes -1 onto the stack.

-1

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 4 bytes

?12?

prints 12\n\n forwards and 21\n\n backwards

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Triangular, 3 bytes

i.%

Try it online!

Prints 1.

%.i prints 0.

If it weren't invalid to do an integer and a character, %# would've been a nice 2-byter.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

@, 2 bytes

Outputs \n1, where \n represents a literal newline.

\1

Reversed

Returns 1, the expression after 1 is ignored.

1\

Try it online!

| improve this answer | |
\$\endgroup\$
1
2

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.