18
\$\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
    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
    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
    Apr 9 at 18:54
  • 1
    \$\begingroup\$ Can I output the zero via exit code? \$\endgroup\$ 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
    Apr 11 at 10:49

60 Answers 60

10
\$\begingroup\$

Nekomata, 2 bytes

Ħ¬

Attempt This Online!

Ħ is a new built-in in Nekomata v0.5.1, so I am using the staging version of ATO.

Ħ¬      Take 5 as an example
Ħ       Histogram; generate a list where the ith element (0-indexed) is the number of times i appears in the input
            5 -> [0,0,0,0,0,1]
 ¬      Logical NOT
            [0,0,0,0,0,1] -> [1,1,1,1,1,0]
\$\endgroup\$
6
\$\begingroup\$

Python 2, 20 bytes

-3 byte thanks to @Jonathan Allan

print'1'*input()+'0'

Attempt This Online!

Python, 27 bytes

print('1'*int(input())+'0')

Attempt This Online!

Python, 25 bytes

print('1'*int(input()),0)

Uses a less strict output format. Thanks to @ayreguitar and @None1

Attempt This Online!

\$\endgroup\$
11
  • \$\begingroup\$ Not sure about this but could you use python -u and save the 4 bytes for the explicit 0? \$\endgroup\$
    – loopy walt
    Apr 10 at 16:23
  • \$\begingroup\$ @loopywalt, I don't see how -u would help implicitly outputting a zero; it seems to be for unbuffering output stream. \$\endgroup\$ Apr 11 at 0:43
  • \$\begingroup\$ I was thinking of the exit code. OP seems to suggest something like that may be allowed. "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." I just was unsure whether buffering might upset the correct order. \$\endgroup\$
    – loopy walt
    Apr 11 at 5:00
  • 1
    \$\begingroup\$ Making us of the 'Any unambiguous output format is allowed' in the original post comments, you can shave off 1 byte: print(' 1'*int(input()),0) \$\endgroup\$
    – ayreguitar
    Apr 13 at 11:24
  • 1
    \$\begingroup\$ @None1, Thanks; For future reference users here prefer comments over suggested edits \$\endgroup\$ Apr 14 at 14:13
5
\$\begingroup\$

JavaScript (ES11), 16 bytes

-4 bytes thanks to @Unmitigated

Expects a BigInt and returns a string.

n=>10n**n/9n+"0"

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ You don't need to subtract 1: n=>10n**n/9n+"0" \$\endgroup\$ Apr 10 at 5:25
  • 1
    \$\begingroup\$ @Unmitigated D'oh. Of course :) Thanks! \$\endgroup\$
    – Arnauld
    Apr 10 at 6:12
  • \$\begingroup\$ An alternative 16 that only works up to n=9 due to integer overflow: x=>10**-~x/9-1|0 \$\endgroup\$
    – emanresu A
    Apr 16 at 23:33
5
\$\begingroup\$

JavaScript (Node.js), 25 bytes

-2 bytes thanks to @DLosc

x=>((2<<x)-2).toString(2)

Try it online!

Takes the number as the parameter to the function, then outputs the binary form of \$2^{x+1}-2\$

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Nice approach! You can save two bytes with 2<<x \$\endgroup\$
    – DLosc
    Apr 10 at 4:50
5
\$\begingroup\$

Brachylog, 4 bytes

⟦ṡᵐ↔

Try it online!

Unifies the output with a list, e.g. [1,1,1,1,1,0]

Explanation

⟦      Range [0, …, Input]
 ṡᵐ    Map sign: 
         The sign of positive integers is 1
         Thankfully for this challenge, the sign of 0 is 0
   ↔   Reverse the list
\$\endgroup\$
4
\$\begingroup\$

Jelly, 3 bytes

>ŻY

A full program that accepts a (non-negative*) integer, \$x\$, and prints \$x\$ ones followed by a zero.

* negative integers will do the same as zero.

Try it online!

How?

>ŻY - Main Link: non-negative integer, X   e.g. 0      3
 Ż  - zero range                                [0]    [0,1,2,3]
>   - {X} greater than {that}?                  [0]    [1,1,1,0]
  Y - join with newline characters
    - implicit, smashing print
\$\endgroup\$
4
\$\begingroup\$

Perl 5 -p, 9 bytes

$_=1x$_.0

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Great answer. Only improvement I can think of is to use -l060 and drop the .0 but it's kinda cheaty! \$\endgroup\$ Apr 11 at 16:13
4
\$\begingroup\$

Knight (v2), 7 bytes

O+*,1P0

Try it online!

This answer takes full advantage of Knight's type coercion.

If you look at the code, you may notice that we seem to be multiplying (*) a list (,1) with a string (P), but due to type coercion, the second argument, which is P, is actually coerced to an integer, so this results in a list that consists of P 1's. Then, we seem to be adding the resulting list to an integer 0, but due to type coercion, the integer is coerced into a list, which, in this case, converts the 0 to a singleton list consisting of a 0. This singleton list is then concatenated at the end of the aforementioned list of 1's, resulting in a list of P 1's, followed by a single 0. Finally, O always coerces its input, which is a list, to a string, resulting in the list being joined by newline before being outputted to stdout.

\$\endgroup\$
4
\$\begingroup\$

Quipu, 32 bytes

1&0&1&/\
--[]/\
1&3&0&
>===??
\/

Attempt This Online!

Explanation

A Quipu program is composed of threads, vertical 2-character-wide strips that act as code and also as value storage. Threads are, by default, executed one at a time left to right. Each thread's value starts at 0.

Thread 0 stores the current value of the loop counter:

1&
--  # Subtract 1 from the previous value of this thread
1&  
>=  # If the result is >= 0, jump to thread 1
\/  # Otherwise, read a number and set thread 0's value to that

Thread 1 checks whether the loop counter has hit zero:

0&
[]  # Load value of thread 0
3&
==  # If the result is == 0, jump to thread 3

Thread 2 is the loop body:

1&
/\  # Output 1
0&
??  # Jump back to thread 0

Thread 3 runs after the loop exits:

/\  # Output this thread's value (0 by default)
\$\endgroup\$
4
\$\begingroup\$

Haskell, 42 37 bytes

-5 bytes thanks to Wheat Wizard.

main=do n<-readLn;print$10^n`div`9*10

Try it online!

Not math, 42 bytes

main=interact(\n->('1'<$[1..read n])++"0")

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ @WheatWizard How... imperative. :P Thanks! \$\endgroup\$ Apr 10 at 4:08
4
\$\begingroup\$

BitCycle -u, 10 bytes

?~
v~v
>!<

Try it online!

This somehow works even though it uses some sketchy undefined behavior. It doesn't work on the Javascript online interpreter though, so it has to be modified for that interpreter:

16 bytes

?~
v~v
>v<
A/
 !

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Undefined behavior FTW. :) Here's a 10-byte version that works in the JS interpreter. \$\endgroup\$
    – DLosc
    Apr 16 at 2:02
  • \$\begingroup\$ @DLosc Ah, flipping it upside down. That's smart. \$\endgroup\$
    – Aiden Chow
    Apr 16 at 18:52
3
\$\begingroup\$

Vyxal, 2 bytes

ʀ>

Try it Online!

Outputs as a list.

Explained

ʀ>ṅ­⁡​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁣‏‏​⁡⁠⁡‌­
ʀ    # ‎⁡                0..x
 >   # ‎⁢[x > n for n in     ]
  ṅ  # ‎⁣join into a single string
💎

Created with the help of Luminespire.

\$\endgroup\$
3
\$\begingroup\$

MathGolf, 3 bytes

Ä10

Try it online.

Explanation:

Ä    # Loop the (implicit) input-integer of STDIN amount of times,
     # using 1 character as inner code-block:
 1   #  Push a 1 every iteration
  0  # After the loop: push a 0
     # (after which the entire stack is joined and output implicitly as result)
\$\endgroup\$
3
\$\begingroup\$

Google Sheets, 13 bytes

=REPT(1,A1)&0
\$\endgroup\$
3
\$\begingroup\$

SQL (Oracle), 31 bytes

Always fun when we can slip an SQL solution that actual isn't that bad:

select lpad(0,&n+1,1)from dual;
\$\endgroup\$
2
3
\$\begingroup\$

Bash, 22 bytes

echo $((10**-~$1/9-1))

Attempt This Online!

Bash, 22 bytes

yes 1|head -n$1;echo 0

Attempt This Online!

\$\endgroup\$
3
\$\begingroup\$

Piet + ascii-piet, 18 bytes (2×9=18 codels)

tabitkcmA ?cjsc aa

Try Piet online!

If we didn't have to consider the x=0 case, there is a nice 14 bytes answer:

tkvnM?esuA Q??

Try Piet online!

\$\endgroup\$
3
\$\begingroup\$

TI-BASIC (TI-84+CE), 13 12 10 bytes

  • Removed one byte thanks to @KaiBurghardt.
  • Removed two bytes by not specifying the increment (which is an optional argument)
seq(X<Ans,X,0,Ans

This creates a list with the correct number of 1's and 0's. The byte count is subtracting any bytes that are present in an empty program. To find the byte count, I looked at the memory stats on the calculator. See how to score TI-BASIC for more information.

The input is in the form of the Ans variable, which needs to be set before running the program. Here is an example of input and output on the TI-84 Plus CE:

enter image description here

\$\endgroup\$
5
  • \$\begingroup\$ You will need to insert a Prompt Z. Currently, input via predefined variables is not accepted. \$\endgroup\$ Apr 11 at 22:12
  • \$\begingroup\$ Given that a list is acceptable output, too, you can devise a list expression. For 12 Bytes you get seq(X<Ans,X,0,Ans,1 where Ans is the input to be entered prior program invocation. \$\endgroup\$ Apr 11 at 22:46
  • \$\begingroup\$ @KaiBurghardt Oh, my bad, I'll fix that. Also good idea! \$\endgroup\$ Apr 12 at 0:33
  • 1
    \$\begingroup\$ Unfortunately the increment is apparently not an optional argument in earlier versions of TI‑BASIC. My TI‑82 “Parcus” calculator raises an ERR:ARGUMENT error without it. Now you need to qualify TI‑BASIC to TI‑BASIC (TI‑84+CE) or whatever it is called. I’m not too familiar with TI‑BASIC and its entire history. \$\endgroup\$ Apr 12 at 13:53
  • \$\begingroup\$ Confirmed this works on the slightly older TI-84 Plus C Silver Edition. \$\endgroup\$
    – Bbrk24
    May 19 at 22:08
3
\$\begingroup\$

Acc!!, 87 86 82 bytes

N
Count i while _%96 {
_/96*960+_%48*96+N
}
Count i while _ {
_-960
Write 49-0^_
}

Try it online!

Explanation

Depends on the slightly unofficial behavior of the official interpreter, in which the input always ends with a newline character.

# Accumulator format: value * 96 + next_char
# Read first character
N
# Loop until next_char is EOF (0)
Count i while _%96 {
  # _/96*96*10   Multiply previous value by 10
  # +_%48*96     Add new digit to it (or 10 if character was newline)
  # +N           Read next character
  _/96*960+_%48*96+N
}
# For an input number X, the accumulator is now equal to (X * 10 + 10) * 96
# which is the same as (X + 1) * 960
# Loop until accumulator reaches 0
Count i while _ {
  # Decrement accumulator in steps of 960
  _-960
  # Output '1' if accumulator is still positive, '0' if it is now 0
  Write 49-0^_
}
\$\endgroup\$
3
\$\begingroup\$

Acc!!, 65 bytes

Count i while _%11-10 {
_*10+_%11+N%48
}
Write 49)*(_/11
Write 48

Try it online!

Abuses a bug in the official interpreter

Acc!!, 105 88 85 81 bytes

-5 byte thanks to @DLosc

Count i while _%11-10 {
_*10+_%11+N%48
}
Count i while _+1 {
Write 49-10/_
_-11
}

Try it online!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ This is brilliant! Here's 87 bytes. \$\endgroup\$
    – DLosc
    Apr 18 at 5:34
  • 1
    \$\begingroup\$ 81 bytes \$\endgroup\$
    – DLosc
    Apr 20 at 4:44
  • 1
    \$\begingroup\$ Wow, that's incredible! It took me quite a while to fully grasp all the details. I think I still don't get it all—probably need a few more minutes of staring at the code! \$\endgroup\$ Apr 20 at 5:15
  • 1
    \$\begingroup\$ That's exactly how I felt reading your earlier solutions. ;) I used the same number-input algorithm over here, if you want to look at the explanation. \$\endgroup\$
    – DLosc
    Apr 20 at 5:20
2
\$\begingroup\$

APL+WIN, 6 bytes

Prompts for integer input:

⌽0,⎕⍴1

Try it online! Thanks to Dyalog Classic

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

Brainfuck+2, 11 bytes

;[>+:-<-]>:

Try it online

Explanation

; Input x as decimal number (only 0 to 255 are supported)
[ While x is nonzero
>+:-< Print 1
- Decrement x
]
>: Print 0
\$\endgroup\$
2
\$\begingroup\$

Pip, 6 bytes

LqP1
0

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ The newline can be removed if you use one or both of the preset variables o (1) or i (0). \$\endgroup\$
    – DLosc
    Apr 9 at 19:00
2
\$\begingroup\$

Retina 0.8.2, 7 bytes

.+
$*10

Try it online! Explanation: $* repeats a single character to its right, so the 0 does not get repeated. By default the repeat count is the entire match, which here is the input, so the input is replaced by the desired output.

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

C (GCC), 49 bytes

-4 bytes thanks to @ceilingcat

main(c){for(scanf("%d",&c);~c;)putchar(49^!c--);}

Attempt This Online!

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

05AB1E, 3 4 3 2 bytes

crossed out 4 is still regular 4 ;(

Ý›

+1 byte as bugfix (L results in \$[1,0]\$ for edge-case \$n=0\$)
-1 byte thanks to @lyxal
-1 byte due to a comment of OP ("Any unambiguous output format is allowed"), so it'll output as a list

Try it online.

Explanation:

Ý   # Push a list in the range [0, (implicit) input of STDIN]
 ›  # Check for each value whether the (implicit) input is larger
    # (after which this list is output implicitly as result to STDOUT)
\$\endgroup\$
4
  • 1
    \$\begingroup\$ This fails the 0 test case, which should print 0, but prints 11 instead \$\endgroup\$
    – lyxal
    Apr 9 at 12:47
  • \$\begingroup\$ @lyxal Sometimes L for \$n=0\$ can be pretty annoying in 05AB1E.. Should be fixed. \$\endgroup\$ Apr 9 at 12:51
  • \$\begingroup\$ That's a hard language design choice imo. On one hand, [] is a sensible default because how can you have a range [0, 1]. But on the other hand, there's been times I would have like to have made that design decision to have it [1, 0] :p \$\endgroup\$
    – lyxal
    Apr 9 at 12:53
  • 1
    \$\begingroup\$ Try it online! for 3 bytes \$\endgroup\$
    – lyxal
    Apr 9 at 12:59
2
\$\begingroup\$

FreePascal, 58 B 54 B

Evidently, the output is just \$2^{x+1}-2\$ written to a base of two. This is shorter than producing the desired output iteratively.

var x:word;begin read(x);write(binStr(1<<(x+1)-2,x+1))end.

−4 B: As pointed out by dtanku, \$2^{x+1}-2 = 2^x2^1-2\$ so we pre‑evaluate \$2^1\$ and start exponentiating from \$2\$.

var x:word;begin read(x);write(binStr(2<<x-2,x+1))end.

NB: The read and its sister procedures accept all integer literals acceptable in FreePascal source code, thus $F is valid input, too.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Can golf a bit further by replacing your 1 by 2 in the bitshift and removing (x+1). \$\endgroup\$
    – dtanku
    Apr 10 at 5:31
2
\$\begingroup\$

Uiua 0.10.0, 3 bytes

0¬⊚

-1 byte thanks to @noodle man

Returns a list of 1s then a 0.

Other solutions

Just to showcase other features in Uiua and different options for output.

0=.⇡
0↯:1
⇌±⇡+1
>⇡+1.
¬⋯ⁿ:2
⇌[@0⍥@1]
0⍥&p‿1
0⍥1      #cannot be bound to a function name
\$\endgroup\$
3
  • \$\begingroup\$ First one is in reverse order \$\endgroup\$
    – noodle man
    Apr 13 at 2:52
  • \$\begingroup\$ You're right; I'll fix it \$\endgroup\$
    – Tbw
    Apr 13 at 4:07
  • \$\begingroup\$ =.⇡ can be not where \$\endgroup\$
    – noodle man
    Apr 13 at 12:09
2
\$\begingroup\$

99, 69 bytes

 999
99 9 9
9999 9 99 9






 9999 999
9
999 999 9
 9 99





9 99
9

Try it online!

This can 100% be golfed but it is very confusing to golf in this language, with all the 9's and all.

The main issue is this: the only way to access loops and conditionals in this language is through its GOTO command, which will conditionally GOTO a specified line. But because variables in 99 can only hold multiples of 9, so the GOTO command can only jump to lines with line numbers that are multiples of 9. That is why there is so much empty space between certain sections of the code: it is to accommodate for the limitations of the GOTO command.

For now, I am using a GOTO for lines 9 and 18, so that the loop starts at line 9, but I feel that there is a way to cut out the GOTO to line 18 and instead have a GOTO to line 0 (making the start of the loop at line 0), I'm just not sure how to go about doing it. The reason why I don't have a GOTO for line 0 is because line 0 is 999, which takes in input and sets it to the variable 999. If the input command was no-op after reading all the input, then I could have a GOTO to line 0, but instead it reads a 0 into the variable, which messes up the entire loop.

I'm also confident that re-arranging the way I assign the variables can also save 2 or 3 bytes, but I don't see anything for now.

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

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.