Skip to main content
Added some info about Martin's suggested shorter solution.
Source Link
James Holderness
  • 8.9k
  • 2
  • 23
  • 44

Befunge, 11 bytes

&:0`\0\`-.@

Try it online!

This is just the obvious (N > 0) - (0 < N) calculation.

&               Read N from stdin.
 :              Make a duplicate copy.
  0`            Calculate N > 0.
    \           Swap the second copy to the top of the stack.
     0\`        Calculate 0 > N.
        -       Subtract the two comparisons: (N > 0) - (0 < N)
         .@     Output the result and exit.

As Martin Ender pointed out, there's potentially a 2-byte shorter solution, using the same idea as his ><> answer:

1~"/"-%.@

Unfortunately this only works if the result of a modulo operation takes the sign of the divisor, which is not that common in Befunge implementations (in particular the reference interpreter doesn't work this way).

1               Push 1 onto the stack for later use.
 ~              Read a character of input (this will be '-' or an ASCII digit).
  "/"-          Subtract 47.
      %         Take the modulo of the 1 we pushed earlier with this difference.
       .@       Output the result and exit.

If you want to try this out you'll probably need to use one of the Python-based interpreters like PyFunge or Befungee. I suspect Fungi might work too.

Befunge, 11 bytes

&:0`\0\`-.@

Try it online!

This is just the obvious (N > 0) - (0 < N) calculation.

&               Read N from stdin.
 :              Make a duplicate copy.
  0`            Calculate N > 0.
    \           Swap the second copy to the top of the stack.
     0\`        Calculate 0 > N.
        -       Subtract the two comparisons: (N > 0) - (0 < N)
         .@     Output the result and exit.

Befunge, 11 bytes

&:0`\0\`-.@

Try it online!

This is just the obvious (N > 0) - (0 < N) calculation.

&               Read N from stdin.
 :              Make a duplicate copy.
  0`            Calculate N > 0.
    \           Swap the second copy to the top of the stack.
     0\`        Calculate 0 > N.
        -       Subtract the two comparisons: (N > 0) - (0 < N)
         .@     Output the result and exit.

As Martin Ender pointed out, there's potentially a 2-byte shorter solution, using the same idea as his ><> answer:

1~"/"-%.@

Unfortunately this only works if the result of a modulo operation takes the sign of the divisor, which is not that common in Befunge implementations (in particular the reference interpreter doesn't work this way).

1               Push 1 onto the stack for later use.
 ~              Read a character of input (this will be '-' or an ASCII digit).
  "/"-          Subtract 47.
      %         Take the modulo of the 1 we pushed earlier with this difference.
       .@       Output the result and exit.

If you want to try this out you'll probably need to use one of the Python-based interpreters like PyFunge or Befungee. I suspect Fungi might work too.

Source Link
James Holderness
  • 8.9k
  • 2
  • 23
  • 44

Befunge, 11 bytes

&:0`\0\`-.@

Try it online!

This is just the obvious (N > 0) - (0 < N) calculation.

&               Read N from stdin.
 :              Make a duplicate copy.
  0`            Calculate N > 0.
    \           Swap the second copy to the top of the stack.
     0\`        Calculate 0 > N.
        -       Subtract the two comparisons: (N > 0) - (0 < N)
         .@     Output the result and exit.