Given a number N, output the sign of N:
- If N is positive, output 1
- If N is negative, output -1
- If N is 0, output 0
N will be an integer within the representable range of integers in your chosen language.
|
C,
|
|
@betseg That is because upvotes on built-ins are now frowned upon.
– Erik the Outgolfer
Dec 20 at 11:41
|
||
|
I was hoping for "Trivial solutions are frowned upon", but well.. Better than nothing. :)
– Stewie Griffin
2 days ago
|
||
|
|||
|
|||
|
@GB Size of int is probably either 2 (16, x86) or 4 (32, x86_64) but remember, all that's needed is an architecture on which it is valid. This isn't Stack Overlflow, portability is not important.
– cat
yesterday
|
Retina, 9 bytes
Replaces a non-zero digit and everything after it with |
|||||
|
COW,
|
Mathematica, 4 bytes
How about not using the built-in
|
|||||
|
Cubix, 10 bytes
This code is wrapped to the following cube net:
The code is then run with the IP (instruction pointer) starting on the The next command is
If the input is negative, the IP turns left at the
The TOS is now If the input is positive, the same thing happens as with negative inputs, with one exception: the IP turns right instead of left at the |
|||||||||||||||||
|
JavaScript (ES6), 9 bytes
Straightforward. The shortest non-builtin is 13 bytes:
Thanks to @Neil, this can be golfed by a byte, but at the cost of only working on 32-bit integers:
Or you could do
which seems more golfable, but I'm not sure how. |
|||||||||||||||||||||
|
Labyrinth, 10 bytes
ExplanationLabyrinth's control flow semantics actually give you a "free" way to determine a number's sign, because the chosen path at a 3-way fork depends on whether the sign is negative, zero or positive. However, I haven't been able to fit a program with junctions into less than 12 bytes so far (although it may be possible). Instead, here's a closed-form solution, that doesn't require any branches:
The instruction pointer then hits a dead end, turns around and terminates when Doubling the input is necessary to make this work with inputs |
|||||||||
|
><>,
|
Dyalog APL, 1 byteWorks for complex numbers too, returning 1∠θ:
Without that built-in:
|
|||||||||||||
|
Vim, 22 bytes
Saved one byte thanks to @DJMcMayhem! Here, As @nmjcman101 pointed out in the comments, a single regex can be used ( Explanation:
Here's a gif of it running with a negative number (old version): Here's it running with 0: Running with positive: |
|||||
|
|
|
|||||||||||||||||||||
|
PHP, 16 bytesUses the new spaceship operator.
|
|||||||||||||||||||||
|
Brain-Flak
|
|
|
|||
|
|
|||
|
|
Perl, 9 bytesRequires
Usage
I'm happy with the fish operator! |
|||||||||||||
|
///, 52 bytes
Ungolfed, explanation:
It's basically a MapReduce implemenatation, i.e. there are two phases:
If there was a |
|||||
|
MATL, 6 bytes
Input may be a number or an array. The result is number or an array with the corresponding values. Try it online! Or test several cases using array input. ExplanationThis avoids using the builtin sign function (
|
|||||||||
|
|
|
|||||
|
R, 25 bytes
Takes the number to STDIN. Then checks if it's zero, if not, returns This is without using the builtin |
|||||||||
|
Octave,
|
|
|
@LuisMendo
N will be an integer I'm lucky it says that in the question :)
– Kritixi Lithos
Dec 20 at 13:59
|
||
|
|
|||
|
Stack Cats, 6 + 4 = 10 bytes
+4 bytes for the
Try it online! As with basically all good Stack Cats golfs, this was found by brute force, beat any manual attempts by a long shot, and can't easily be incorporated into a larger program. Add a Stack Cats uses a tape of stacks which are implicitly filled with zeroes at the bottom. At the start of the program, all input is pushed onto the input stack, with a The relevant commands here are:
Note that all of these commands are invertible, with its inverse being the mirror of the command. This is the premise of Stack Cats — all nontrivial terminating programs are of odd length, since even length programs self-cancel. We start with
Now we branch based on
|
|||
|
|
V
|
|
|
I knew I should have read that page better. It's not actually shorter - I forgot 0, but I was trying to take the number in as an argument and then
Àé1. A positive number gives a string of ones, a negative number SHOULD give a string of ones one row up, and 0 would give nothing. The negative number bit didn't work with À, but did with d$@"
– nmjcman101
Dec 20 at 17:27
|
||
|
|
Ah. Well the reason that doesn't work is because there isn't another row up for it to go onto. If you add
é<cr> it'll have two empty lines and then that does work. I'm not sure if you can use that to get a full answer
– DJMcMayhem
Dec 20 at 17:40
|
||
|
|
I did have another line to up to, just didn't explicitly say that in my comment. What's the
-- argument you added?
– nmjcman101
Dec 20 at 17:47
|
||
|
It means "end of options". Since
-6 starts with a flag, docopt (the python library for command line options) thinks it's a command-line flag rather than an argument. Adding -- just signals that it's an argument not an option. Otherwise, it won't run at all because of invalid command line invocation.
– DJMcMayhem
Dec 20 at 17:49
|
C#,
|
|
|||
|
|||
|
|
Ruby, 10 bytes
|
|||
|
|
Jelly, 1 byte
The monadic sign atom, |
||||
|
|
TI-Basic, 8 bytes
Alternative solutions (feel free to suggest more):
|
|||||||||||||||||||||
|
Actually, 1 byte
Another case of exactly what it says on the tin - Without the builtin (4 bytes):
|
||||
|
|
Mathematica, 4 bytes
Exactly what it says on the tin |
|||||||||||||||||
|
|
|
|||
|
|
Labyrinth, 8 bytes
I'm posting this as a separate answer because my other Labyrinth answer is based on arithmetic on the actual numerical input value, whereas this mostly ignores the number and works with the character code of the first character instead. ExplanationSo yeah, this reads the first character code which is either 45 (
The instruction pointer then hits a dead end, turns around and terminates when % now attempts a division by zero. Another simple computation that works:
|
|||
|
|
Mathematica, 28 bytes
Mathematica, 30 bytes
Mathematica, 76 bytes
Just to be different :) |
|||||||||||||||||
|