-6
\$\begingroup\$

This is a very simple challenge, and it is to print the numbers -1, 0, and 1.

The following paragraph is credited to Print numbers from 1 to 10
Your output format can be whatever your language supports. This includes arbitrary separators (commas, semicolons, newlines, combinations of those, etc., but no digits), and prefixes and postfixes (like [...]). However, you may not output any other numbers than -1 through 1. Your program may not take any input. Standard loopholes are disallowed.

This is , so the shortest answer in bytes wins. Happy golfing!

\$\endgroup\$
8
  • 7
    \$\begingroup\$ I don't think we need one of these for all number ranges. \$\endgroup\$
    – Dennis
    Commented Feb 21, 2018 at 2:56
  • \$\begingroup\$ @Dennis but all the others start at 0 or 1. plus, I'd also like to see if any get it in 2 bytes or less \$\endgroup\$
    – robbie
    Commented Feb 21, 2018 at 3:00
  • 4
    \$\begingroup\$ Starting at -1 might be annoying for languages that have only unsigned integers, but it will make no difference whatsoever for all others. If you want to satisfy your 2-byte curiosity, you can join us in The Nineteenth Byte. \$\endgroup\$
    – Dennis
    Commented Feb 21, 2018 at 3:06
  • 2
    \$\begingroup\$ Does this count? \$\endgroup\$ Commented Feb 21, 2018 at 7:00
  • \$\begingroup\$ @ETHproductions yes! \$\endgroup\$
    – robbie
    Commented Feb 22, 2018 at 1:54

15 Answers 15

3
\$\begingroup\$

Jelly, 3 bytes

-r1

Try it online!

1r-

Try it online!

-ŒR

Try it online!

1ŒR

Try it online!

Ø-Ż

Try it online!

Ø+Ż

Try it online!

-rN

Try it online!

1rN

Try it online!

2Ż’

Try it online!

3Ḷ’

Try it online!

and a bonus one:

M, 2 bytes

-R

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ This is what we call "flexing". Have my upvote. \$\endgroup\$ Commented Apr 13, 2021 at 17:01
2
\$\begingroup\$

Pyt, 4 bytes

3ř⁻⁻

Try it online!

Explanation:

3      Push 3
ř      Push [1,2,3]
⁻⁻     Decrement twice
       Implicit print
\$\endgroup\$
2
\$\begingroup\$

R, 9 bytes

cat(-1:1)

Try it online!

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

7, 11 characters, 4 bytes

17370074023

Try it online!

Hexdump to prove byte count:

00000000: 3df0 3c09                                =.<.

This challenge is very easy in most languages, so I decided to solve it in a language where it's actually an interesting problem.

Explanation

It took me ages to golf this down to 4 bytes – there are a really huge number of ways to do this with 12 characters, but reaching 11 is very difficult.

The only part of the code here that actually runs (after the first cycle, which just sets up the stack to mirror the structure of the program) is 4623 (which is the code generated by the code 4023 in the original program; the 4023 is a literal which pushes 4623 to the stack; as usual, I'm using non-bold for commands that just push code to the stack and bold for the commands they push). After the first cycle, each new cycle runs the top stack element, without actually popping it from the stack.

The 4 swaps the top two stack elements, and inserts a blank element between them (thus, a b goes to b blank a). Then the 6 analyzes the top element to determine what source code is most likely to have produced it, and appends the resulting source code to the element below it (which will be blank at this point because we just ran a 4). Thus, the net effect of 46 is to swap the top two stack elements, and replace the top stack element with (a guess at, but the guesses are always correct for this program) the source code that produced it.

2 duplicates the top stack element, and 3 outputs the top stack element, popping both it and the element below. Thus, the net effect of 23 is to output and pop the top stack element, without disturbing elements below it.

As such, the effect of our program 4623 is to pop and output the source code that produced the second stack element, whilst leaving the top stack element unchanged. The top stack element in question is the actively running program (which doesn't get popped when we start to run it), so once the program finishes running, it'll just run again, etc. – this is in effect an infinite loop which, for each element on the stack, outputs the source code that produced it (and eventually the program crashes due to stack exhaustion). As such, the data being given to the output routines is 00, 3, 1 (i.e. the sections of the source code in between 7s – the source code has already run to produce runnable code by this point, but the source is reconstructed from the runnable code rather than the runnable code actually being run).

The first 0 sent to the output routine selects numerical output mode. Then, 0, 3, 1 get output. The numerical value of a string in 7 is the total number of 1 and 7 characters it contains, minus the total number of 0 and 6 characters it contains. Thus, the values of these strings are -1, 0, and 1, as required by the question.

One complication is that if we try to output an empty string, we just get no output, rather than its numerical value of 0 being output: this is a special case needed because the output command 3 is the primary way to pop and discard stack elements (basically, you push an empty element above the one you want to pop, output that empty element and pop the element below as a side effect). A "clean" way to output 0 thus requires outputting a string like 01 which is nonempty and has a numerical value of zero. This program uses 3, which is shorter, but the numerical output formatter will append a comma when outputting strings ending 3 (in addition to outputting their numerical value). As a consequence, the output of the program is actually -1 0, 1, but this is permitted by the question. (All eight of the one-character strings either have a side effect when output via the numerical formatter, or else have a value different from zero, so outputting -1 0 1 exactly would make the program longer.)

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

Brainfuck, 43 bytes

+++++++++[>+++++>+<<-]>.++++.>++++.<-.+>.<.

I'm not good at brainfuck, so its fun to make these and hope i'm not overcomplicating it :3

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

Python 2, 9 bytes

print-101

Try it online!

There is nothing disallowing you to use the empty string as a separator…

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

HTML, 2 bytes

10

  • Nobody said you couldn't use base -1.
  • Nobody said you couldn't use the same digit for multiple numbers.

  • 1 is represented as 1.
  • 0 is represented as 0.
  • -1 is represented as 10, as this is in base -1.
\$\endgroup\$
1
\$\begingroup\$

Brainfuck, 41 bytes

-[+>+[+<]>+]>-[->+>+<<]>.++++.>-.<-.>.<+.

Attempt This Online!

Uses comma , as a separator, however the question doesn't really specify if we need a separator so..

Brainfuck, 24 bytes

-[+>+[+<]>+]>-.++++.-.+.

Attempt This Online!

Brainfuck+ (Modulo256), 29 27 bytes

-[+>+[+<]>+]>-.-<+:>.>:<.<:

Also uses a comma , as a separator, what's different about Modulo256 Brainfuck+ is that we can use the : operator to output the value of a cell instead of its ascii character.

And then again, without separators:

Brainfuck+ (Modulo256), 22 bytes

-[+>+[+<]>+]>-.;+:-:+:
\$\endgroup\$
0
\$\begingroup\$

JavaScript, 19 15 bytes

[-1,0,1].map(alert)

alert([-1,0,1])

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

Python - 15 bytes

print("-1 0 1")

I would say "Good luck improving this.", but past experience has taught me never to assume: though, this won't be easily beat (for i in range(3), %ding the spaces and %sing the 1s are all overweight, so you can eliminate those.)

\$\endgroup\$
5
  • 1
    \$\begingroup\$ -2 bytes \$\endgroup\$ Commented Apr 13, 2021 at 21:16
  • 1
    \$\begingroup\$ -4 bytes if you switch to Python 2 \$\endgroup\$ Commented Apr 13, 2021 at 21:17
  • \$\begingroup\$ I see. Numeric output requires no quotes. \$\endgroup\$ Commented Apr 13, 2021 at 21:28
  • 3
    \$\begingroup\$ You need commas between the numbers, otherwise this is invalid syntax, and you can remove the space before the - \$\endgroup\$ Commented Apr 13, 2021 at 21:33
  • \$\begingroup\$ As pointed out above, your answer was invalid due to the broken syntax. I've rolled it back to your previous valid answer. Feel free to implement @Zionmyceliaadamancy's suggestions properly this time \$\endgroup\$
    – Jo King
    Commented Jul 14, 2022 at 0:36
0
\$\begingroup\$

Pascal, 41 Bytes

According to ISO standard 7185 “Standard Pascal” integer parameters to write/writeLn have an implementation‑defined default minimum printing width, provided the write destination is a text file (such as the program parameter output is).

program p(output);begin write(-1,0,1)end.

Expanded:

program signumCodomain(output);
    begin
        writeLn(output, −1, 0, +1);
        { which translates to
          
        writeLn(output, −1:integerDefaultMinimumWidth,
                         0:integerdefaultMinimumWidth,
                        +1:integerdefaultMinimumWidth);
        
        where `integerDefaultMinimumWidth` stands for an inaccessible
        implementation‑defined `integer` value greater than or equal to `1`. }
    end.

Presuming integerDefaultMinimumWidth was 11, the …Ln‑flavored program prints:

         -1          0          1

Users of the FreePascal Compiler (FPC) need to insert a {$mode ISO} compiler directive in front of program, or specify the ‑MISO parameter to the command‐line of fpc. Otherwise the default minimum width is 1, which just smushes all integer values together into one string without any separation.

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

YASEPL, 8 bytes

=i-<+<+<

translated to javascript:

let i = -1;
console.log(i);
i = i + 1;
console.log(i);
i = i + 1;
console.log(i);
\$\endgroup\$
0
\$\begingroup\$

Punchcode, 7 bytes



heres the punchtape text (uncompiled punchcode), with explanations

-START  |
---OOO-O| set first item in tape to 0
---OOO--| subtract it by 1
----O-OO| print
---OO-OO| add 1
----O-OO| print
---OO-OO| add 1
----O-OO| print
\$\endgroup\$
0
\$\begingroup\$

gammascript, 49 12 bytes

write -1 0 1

cheesy. But since there's no other way to make it shorter, this works

try it online (the double newlines aren't really necessary in the code)

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

AWK, 16 bytes

END{print"-101"}

Some AWK implementations will need BEGIN instead of END, however.

You could do it at 9 bytes, with an empty input:

awk '$0="-101"' <<< ""
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.