19
\$\begingroup\$

Fixed Repeating Output is a challenge originally invented by Esolang user AnotherUser05 which shows that a language supports loops and I/O.

Challenge

A program that solves the challenge has to do these steps in order:

  • Reads a decimal integer x from any sort of input (e.g.: STDIN, file).
  • Then, print 1 for x times to any sort of output (e.g.: STDOUT, file).
  • Then, print 0 to any sort of output (e.g.: STDOUT, file). As an exception, outputting the 0 via exit code is not allowed because that would be unfair.

For example, if the input is 5, then:

1
1
1
1
1
0

and

111110

are both valid outputs, but

101111

is not a valid output.

A program doesn't have to output the 1's and the 0 to the same destination, but it still has to print the 1's first, then the 0. The behavior when x is negative is undefined.

This is , so shorter program wins.

\$\endgroup\$
6
  • 1
    \$\begingroup\$ What if x is 0? And does it explicitly have to be a program using input and output, or would just a function returning a string count? \$\endgroup\$
    – Tbw
    Commented Apr 9 at 12:38
  • 1
    \$\begingroup\$ When x is 0, it prints 0 because printing 1 zero times is the same as doing nothing. For the second question, I'm not the original author of the challenge so let's say that it counts. \$\endgroup\$
    – None1
    Commented Apr 9 at 12:46
  • \$\begingroup\$ How flexible is the output format? For example, in my language BitCycle, the straightforward method to input a decimal integer and output 1s and 0s would result in 1,1,1,1,1,0 as the output. What about 1 1 1 1 1 0? Or [1,1,1,1,1,0]? \$\endgroup\$
    – DLosc
    Commented Apr 9 at 18:54
  • 1
    \$\begingroup\$ Can I output the zero via exit code? \$\endgroup\$ Commented Apr 11 at 0:43
  • 4
    \$\begingroup\$ I don't think that's fair, because if you can do that, then this challenge will be the same as outputting x ones. \$\endgroup\$
    – None1
    Commented Apr 11 at 10:49

63 Answers 63

2
\$\begingroup\$

Ouroboros, 9 8 bytes

r+.!.!n(

Try it here! (You'll need to enter the code and input manually.)

Explanation

Due to some convenient coincidences, we can use a single snake for the program. Execution starts at the left end and proceeds to the right before looping back around to the beginning again. When the instruction pointer is on a character that gets swallowed, execution halts.

  • r reads an integer from input and pushes it to the stack. If there is no input remaining, it pushes -1 instead.
  • + adds the top two items on the stack. Missing items are treated as 0.
  • .! duplicates the top of the stack and logically negates it (0 -> 1, anything else -> 0).
  • n outputs the top of the stack as a number.
  • ( takes the top of the stack and eats that many characters from the end of the snake. Since execution is on the final character, if a nonzero number of characters is eaten, the program halts.

So: On the first iteration, r+ reads the input number and adds it to 0; on subsequent iterations, it pushes -1 and adds it to the existing number on the stack, decrementing it. .!.! pushes 0, then 1 if the stack number has not yet reached zero; otherwise, it pushes 1, then 0. n outputs the second of these numbers, and ( halts the program if the first is 1.

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

!@#$%^&*()_+, 28 bytes

>_(>+$!+!!+!+++n_*)+(!#_+)#

Try it online!

Input is a non-negative integer followed by the character "n". This is allowed as per this default I/O method (in this case, the input is actually an array of characters with the sentinel value being the character "n").

The character after the second ( is an ASCII 1 character.

The first loop is a golfed version of the "Input a Decimal Number" algorithm in this Esolangs article, using "n" as a sentinel value instead of a newline.

\$\endgroup\$
3
  • \$\begingroup\$ Could you save two bytes with null-terminated input? \$\endgroup\$
    – emanresu A
    Commented Apr 18 at 22:07
  • \$\begingroup\$ @emanresuA hmm maybe, i might be able to cut out n_. \$\endgroup\$
    – Aiden Chow
    Commented Apr 19 at 2:57
  • \$\begingroup\$ Well actually i would still need to push a 0 i think, so maybe 1 byte save \$\endgroup\$
    – Aiden Chow
    Commented Apr 19 at 2:58
2
\$\begingroup\$

Ly, 5 bytes

R[Lu]

Try it online!

I found 5 different Ly programs that met the challenge in 6 characters. Then I accidentally stumbled onto this one...

R      - generate a sequence from 0..N (from STDIN)
 [  ]  - loop while the top of stack is non-zero
  L    - "less than" compare top two entries (will be 1)
   u   - print the top of stack
\$\endgroup\$
2
\$\begingroup\$

Trilangle, 15 bytes

?<.>.(),/.!/'1!

Playground link

Issues warnings (in the online interpreter or with -w set) and exits with an error.

Unfolded:

    ?
   < .
  > . (
 ) , / .
! / ' 1 !

Roughly equivalent to this C code:

extern int get_int(); // Read stdin until an integer is found, then return that

int main() {
  int i = get_int();
  while (--i >= 0) {
    printf("%d\n", 1);
  }
  printf("%d\n", ++i);
  return 1;
}

Trilangle, no errors, 16 bytes

?<.>.(),/.!/'1!@

Playground link

Same idea as above, but add an exit opcode before it wraps around to the parts that error.

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

Charcoal, 4 bytes

×1N0

Try it online! Link is to verbose version of code. Explanation: × is the repetition operator, so the string 1 is repeated according to the numeric input, and then the 0 is simply appended. Note that other ways of repeating 1 would cause it to become adjacent to the 0, which would require a separator character.

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

JavaScript (Node.js), 18 bytes

n=>'1'.repeat(n)+0

Try it online!

JavaScript (Node.js), 19 bytes

f=n=>n?1+f(--n):'0'

Try it online!

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

F# (.NET 6+), 41 bytes

printf"%B"((2<<<int(stdin.ReadLine()))-2)

Most online compilers are too old (they can't format as binary), but this compiles on a proper IDE.

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

Whitespace, 55 bytes

[S S S N
_Push_0][S N
S _Duplicate_0][T   N
T   T   _Read_STDIN_as_integer][T   T   T   _Retrieve_input_n][N
S S N
_Create_Label_LOOP][S N
S _Duplicate_n][N
T   S T N
_If_0_Jump_to_Label_END][S S S T    N
_Push_1][S N
S _Duplicate_1][T   N
S T _Print_as_integer_to_STDOUT][T  S S T   _Subtract_top_two][N
S N
N
_Jump_to_Label_LOOP][N
S S T   N
_Create_Label_END][T    N
S T _Print_as_integer_to_STDOUT]

Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.

Try it online.

Explanation in pseudo-code:

Integer n = STDIN as integer
Start LOOP:
  If (n == 0):
    Stop LOOP
  Print 1 (as integer to STDOUT)
  Go to next iteration of LOOP
Print n (which is 0 at this point) as integer to STDOUT
\$\endgroup\$
1
\$\begingroup\$

Brainfuck, 25 bytes (non-competing*)

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

Attempt This Online!

*: takes as input as a character-code, e.g. \x20 instead of 32.

\$\endgroup\$
2
  • \$\begingroup\$ Given the "decimal integer" phrasing, I'm not sure if character-code I/O is acceptable or if you're going to have to parse an integer from stdin digit-by-digit. However, if character-code I/O is acceptable, then it stands to reason you can output character codes 1 and 0 instead of ASCII digits 1 and 0... \$\endgroup\$
    – DLosc
    Commented Apr 9 at 19:56
  • \$\begingroup\$ @DLosc you are right, now that I read the challenge more carefully I don't think it's acceptable \$\endgroup\$
    – matteo_c
    Commented Apr 10 at 9:51
1
\$\begingroup\$

*><>, 10 bytes

Uses the -i flag to pass in input.

:?1:n?!;1-

Explanation

:           Duplicate the top value on the stack
 ?1         Add 1 to the stack if the top value isn't zero         
   :        Duplicate the top value of the stack
    n       Print the top value as a number
     ?!;    Halt execution if the top value is 0
        1-  Subtract 1 from the top value

Try it online!

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

C++, 68 bytes (-9 by ceilingcat, and -1 yet again by Mukundan314)

#import<iostream>
main(int c){for(std::cin>>c;~c;)putchar(49^!c--);}

I'm…not even going to try to explain this. Ooh, XOR!

C++ (gcc), 78 bytes (-4 thanks to Mukundan314)

#include<iostream>
int main(){int c;for(std::cin>>c;c--;)puts("1");puts("0");}

Similar idea to the original, but using for loops and puts.

C++ (gcc), 82 bytes (OG)

#include<iostream>
int main(){int n;std::cin>>n;std::cout<<std::string(n,'1')<<0;}­⁡​‎‎

C++ syntax highlighting doesn't work. :(

Explanation

#include<iostream>
int main(){int n;std::cin>>n;std::cout<<std::string(n,'1')<<0;}­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏⁠‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏⁠‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏⁠‎⁡⁠⁤⁢‏⁠‎⁡⁠⁤⁣‏⁠‎⁡⁠⁤⁤‏⁠‎⁡⁠⁢⁡⁡‏⁠‎⁡⁠⁢⁡⁢‏⁠⁠⁠⁠⁠⁠⁠⁠⁠⁠⁠⁠⁠‏​⁡⁠⁡‌⁢​‎‎⁢⁠⁡‏⁠‎⁢⁠⁢‏⁠‎⁢⁠⁣‏⁠‎⁢⁠⁤‏⁠‎⁢⁠⁢⁡‏⁠‎⁢⁠⁢⁢‏⁠‎⁢⁠⁢⁣‏⁠‎⁢⁠⁢⁤‏⁠‎⁢⁠⁣⁡‏⁠‎⁢⁠⁣⁢‏⁠‎⁢⁠⁣⁣‏⁠‎⁢⁠⁤⁤⁣‏‏​⁡⁠⁡‌⁣​‎‎⁢⁠⁣⁤‏⁠‎⁢⁠⁤⁡‏⁠‎⁢⁠⁤⁢‏⁠‎⁢⁠⁤⁣‏⁠‎⁢⁠⁤⁤‏⁠‎⁢⁠⁢⁡⁡‏⁠‎⁢⁠⁢⁡⁢‏⁠‎⁢⁠⁢⁡⁣‏⁠‎⁢⁠⁢⁡⁤‏⁠‎⁢⁠⁢⁢⁡‏⁠‎⁢⁠⁢⁢⁢‏⁠‎⁢⁠⁢⁢⁣‏⁠‎⁢⁠⁢⁢⁤‏⁠‎⁢⁠⁢⁣⁡‏⁠‎⁢⁠⁢⁣⁢‏⁠‎⁢⁠⁢⁣⁣‏⁠‎⁢⁠⁢⁣⁤‏⁠‎⁢⁠⁢⁤⁡‏‏​⁡⁠⁡‌⁤​‎‎⁢⁠⁢⁤⁢‏⁠‎⁢⁠⁢⁤⁣‏⁠‎⁢⁠⁢⁤⁤‏⁠‎⁢⁠⁣⁡⁡‏⁠‎⁢⁠⁣⁡⁢‏⁠‎⁢⁠⁣⁡⁣‏⁠‎⁢⁠⁣⁡⁤‏⁠‎⁢⁠⁣⁢⁡‏⁠‎⁢⁠⁣⁢⁢‏⁠‎⁢⁠⁣⁢⁣‏⁠‎⁢⁠⁣⁢⁤‏‏​⁡⁠⁡‌⁢⁡​‎‎⁢⁠⁣⁣⁡‏⁠‎⁢⁠⁣⁣⁢‏⁠‎⁢⁠⁣⁣⁣‏⁠‎⁢⁠⁣⁣⁤‏⁠‎⁢⁠⁣⁤⁡‏⁠‎⁢⁠⁣⁤⁢‏⁠‎⁢⁠⁣⁤⁣‏⁠‎⁢⁠⁣⁤⁤‏⁠‎⁢⁠⁤⁡⁡‏⁠‎⁢⁠⁤⁡⁢‏⁠‎⁢⁠⁤⁡⁣‏⁠‎⁢⁠⁤⁡⁤‏⁠‎⁢⁠⁤⁢⁡‏⁠‎⁢⁠⁤⁢⁢‏⁠‎⁢⁠⁤⁢⁣‏⁠‎⁢⁠⁤⁢⁤‏⁠‎⁢⁠⁤⁣⁡‏⁠‎⁢⁠⁤⁣⁢‏‏​⁡⁠⁡‌⁢⁢​‎‎⁢⁠⁤⁣⁣‏⁠‎⁢⁠⁤⁣⁤‏⁠‎⁢⁠⁤⁤⁡‏⁠‎⁢⁠⁤⁤⁢‏‏​⁡⁠⁡‌­

#include<iostream>                                               # ‎⁡Imports the I/O library (char* haters be mad)
int main(){                                                   }  # ‎⁢Initiates the main() function.
           int n;std::cin>>n;                                    # ‎⁣Initializes variable n and reads it from STDIN.
                             std::cout<<                         # ‎⁤Prints things to STDOUT.
                                        std::string(n,'1')       # ‎⁢⁡This constructs a string with the char '1' repeated n times.
                                                          <<0;   # ‎⁢⁢Appends a 0 and ends the final line.
💎

Created with the help of Luminespire.

\$\endgroup\$
1
  • \$\begingroup\$ forgot that garbage security is an option. thanks! \$\endgroup\$
    – Someone
    Commented Apr 9 at 15:47
1
\$\begingroup\$

Octave / MATLAB, 20 19 bytes

@(n)[~(1:n)+49 '0']

Try it online!

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

Ruby, 12 bytes

->n{?1*n+?0}

Try it online!

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

PARI/GP, 12 bytes

n->10^n\9*10

Attempt This Online!

A function.


PARI/GP, 20 bytes

print(10^input\9*10)

Attempt This Online!

A full program.

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

Zsh, 19 bytes

repeat $1 <<<1
<<<0

Try it online!

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

jq, 9 bytes

.*"1"+"0"

Try it online!

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

MarioLANG, 28 bytes

+>);[!:
="===#
 !:(-<
 #==="

Try it online!

Seems almost optimal, if not optimal.

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

BQN, 6 bytes

∾⟜0⥊⟜1

Try it at BQN online!

Explanation

∾⟜0⥊⟜1
    ⥊⟜1  # Reshape 1 to a length given by the function argument
          # I.e. create a list of (function argument) many 1s
∾⟜0      # Append a 0 to this list

I've tried in vain to find a more interesting approach that's shorter. Most end up being 7 bytes; a few are 6 bytes:

∾⟜0⋈⊐↕  # Get indices of range(𝕩) in ⟨𝕩⟩, then append 0
«0∾⥊⟜1  # Reshape 1, prepend 0, shift left
×0∾˜⥊˜   # Reshape self, append 0, get sign of each

Try them

Given N, it's hard to make a list of length N+1 in BQN. The only builtin that does that is , but it requires its argument to be a list (thus wasting two bytes on ∘⋈), and it returns a list of lists, which is inconvenient to transform into a list of integers.

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

Regenerate, 7 bytes

1{$~0}0

Attempt This Online!

$~0 is the first command-line argument. 1{$~0} repeats 1 that many times, and 0 adds a 0 on the end.

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

tinylisp, 33 bytes

(d F(q((N)(i N(c 1(F(s N 1)))(q(0

Defines a function F that takes an integer and returns a list. Try it online!

(d F(q((N)(i N(c 1(F(s N 1)))(q(0))))))
(d F                                  ) ; Define F as
    (q(                             ))  ; a function
       (N)                              ; of one parameter, N:
          (i N                     )    ;  If N is truthy (nonzero):
              (c 1          )           ;   Prepend 1 to
                  (F       )            ;   the result of a recursive call
                    (s N 1)             ;   with argument N-1
                                        ;  Otherwise:
                             (q(0))     ;   A list containing a single 0

This solution has one major problem, the cardinal sin of lisp functions: it is not tail-recursive. For values of N above about 250, it gives a recursion error. So here is a tail-recursive function in 37 bytes that takes an integer, prints that many 1s, and returns 0:

(d F(q((N)(i N(i(disp 1)0(F(s N 1)))0

Try it online!

(d F(q((N)(i N(i(disp 1)0(F(s N 1)))0))))
(d F                                    ) ; Define F as
    (q(                               ))  ; a function
       (N)                                ; of one parameter, N:
          (i N                       )    ;  If N is truthy (nonzero):
                (disp 1)                  ;   Output 1 (returns nil, which is falsey)
              (i                   )      ;   If disp's return value is truthy:
                        0                 ;    0
                                          ;   Otherwise (which is always the case):
                         (F(s N 1))       ;    Recursive call with N-1
                                    0     ;  Otherwise, return 0

And here is a 40-byte tail-recursive anonymous function that returns a list, using some library functions:

(load library
(q((N)(map positive?(to0 N

Try it online!

(q((N)(map positive?(to0 N))))
(q(                         )) ; A function
   (N)                         ; of one parameter, N:
                    (to0 N)    ;  Range from N down to 0
      (map positive?       )   ;  For each, 1 if positive, 0 otherwise
\$\endgroup\$
1
\$\begingroup\$

TacO, 8 bytes

@#*i
0 1

I believe this is optimal, even with the wasted #.

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ my first time playing with a 2D lang, so must have found like 30 different 8 byte solutions before I finally stumbled on a some real 7 byte solutions \$\endgroup\$
    – guest4308
    Commented May 22 at 20:22
1
\$\begingroup\$

Thue, 252 bytes

N::=:::
9)::=)T9
8)::=)T8
7)::=)T7
6)::=)T6
5)::=)T5
4)::=)T4
3)::=)T3
2)::=)T2
1)::=)T1
0)::=)T
9|::=8|1
8|::=7|1
7|::=6|1
6|::=5|1
5|::=4|1
4|::=3|1
3|::=2|1
2|::=|11
1|::=|1
T|::=|T
1T::=T1111111111
()|::=P
PT::=P
P1::=P!1
!1::=~1
P0::=~0
::=
(N)|0

Try it online! (Note that the newline at the end of the input seems to be mandatory; otherwise, the interpreter doesn't read the last character of the input.)

Explanation

Thue is a non-deterministic string-rewriting language. The program comes in two parts: the top section, above the ::= line, is a list of rewriting rules, and the bottom section, below the ::= line, is the initial state. At every step, a rule that can be applied to the current state is picked at random and applied. Once no rules can be applied, the program halts.

Our initial state is:

(N)|0

to which the only rule that can be applied is the first one:

N::=:::

::: is a special replacement that reads a line of stdin. We now have our decimal input number within the parentheses.

9)::=)T9
...
1)::=)T1
0)::=)T

Move the rightmost digit outside of the parentheses and add a T marker indicating that everything to the left of it needs to be multiplied by 10.

9|::=8|1
...
2|::=|11
1|::=|1

Move the digit through the pipe character, converting to unary.

T|::=|T
1T::=T1111111111

Move T through the pipe character as well; anytime there's a 1 to its left, convert to ten 1s on its right.

()|::=P
PT::=P

Once the whole number has been converted, replace everything before it with P for printing mode.

P1::=P!1
!1::=~1

Any time P is followed by 1, insert ! before the 1; any time ! is followed by 1, remove both and output 1.

P0::=~0

Once all the 1s are gone, output 0.

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

sed, 39 38 bytes

s/.*/echo {0..&}/e  #0 to n seperated by spaces
s/[^ ]//g           #only keep the spaces
y/ /1/              #spaces to 1s
a0                  #append a 0

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Unfortunately I get an invalid command error: sed: 1: "/.*/echo ": invalid command code e Do some other side conditions have to be met? Please document so. I see you link GNU sed, but then I get the error sh: 1: sh: 1: cho: not found }/e: not found empty line 0. \$\endgroup\$ Commented Apr 14 at 11:15
  • \$\begingroup\$ @KaiBurghardt my req for 'it works' is 'it works on TIO'. I can't debug every possible implementation. if you want to be perfectly exact, --version on TIO says it uses sed (GNU sed) 4.5. if you want me to help you debug your specific problem, can you tell me exactly how you ran it here? \$\endgroup\$
    – guest4308
    Commented Apr 14 at 11:30
1
\$\begingroup\$

AWK, 27 bytes

{for(;$1+1;)print $1--?1:0}

Try it online!

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

Rust, 98 bytes

fn main(){print!("{}{}","1".repeat(std::env::args().nth(1).unwrap().parse::<usize>().unwrap()),0)}

Attempt This Online!

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

Uiua, 3 bytes

¬°⊚

Explanation

 °⊚ # histogram (4 -> [0 0 0 0 1])
¬   # logical not ([0 0 0 0 1] -> [1 1 1 1 0])

```

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

How dare you fuck the brain, 12 bytes

;^Iv
D^Nv)NH

Try it online

\$\endgroup\$
4
  • \$\begingroup\$ HDYFTB has no input so this is the reason why the amount of bytes is >12 \$\endgroup\$
    – Gleb
    Commented Oct 4 at 14:56
  • \$\begingroup\$ EDIT: ; now exists so HDYFTB has input \$\endgroup\$
    – Gleb
    Commented Oct 8 at 18:07
  • 2
    \$\begingroup\$ Hi! Welcome to CGCC! I think you might find this to be a slightly more readable link. \$\endgroup\$ Commented Oct 8 at 18:30
  • 1
    \$\begingroup\$ yeah i did \$\endgroup\$
    – Gleb
    Commented 17 hours ago
0
\$\begingroup\$

PowerShell Core, 13 bytes

,1*"$input"
0

Try it online!

Outputs to stdout and is newline separated
Uses the $input variable to access stdin

,1*"$input" # generate an array of 1 of the size of $input and print it
0           # print a zero
\$\endgroup\$
0
\$\begingroup\$

Python, 18 bytes

lambda x:"1"*x+"0"

Port of my Rust answer to Python.

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

PingPong, 15 bytes

;#/=\.@
1-/ \1.

Try it online!

I have another 15 bytes answer below, but I put this one at the top because I felt like this had to most golfing potential. I'm pretty confident the answer below is fully golfed using the strategies it employs.

15 bytes

;=\.@
0]\1.1-00

Try it online!

\$\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.