51
\$\begingroup\$

yes, from coreutils, is 91 lines long. Many of them are comments, but that is still WAY too long.

Edit from September 2019: the source file grew over the past five years and is now 126 lines long.

Write a program that imitates yes:

  • outputting to stdout an infinite stream of "y\n"'s
  • there must be an option to stop it other than killing the process with SIGKILL: but SIGINT and SIGPIPE are fine
  • you are not allowed to use "y" or "\n" or their ASCII values (121, 0x79, 0171, 10, 0xA or 012)

Shortest answer wins.

Bonus:

  • subtract 10 from your code length, if you can receive a phrase in stdin and print it out instead of "y" (but still including the line-break). The program doesn't have to print y on an empty input.
\$\endgroup\$
28
  • 13
    \$\begingroup\$ "you are not allowed to use "y" or "\n"" -- should I read this as "You may not use y or \n inside of a string literal"? \$\endgroup\$ – apsillers Dec 17 '14 at 14:28
  • 14
    \$\begingroup\$ On a related note, GNU true.c is 80 lines long. \$\endgroup\$ – Dennis Williamson Dec 17 '14 at 22:32
  • 8
    \$\begingroup\$ @DennisWilliamson On a similarly related note, false.c is 2 lines long.... ;_; \$\endgroup\$ – LordAro Dec 18 '14 at 2:30
  • 7
    \$\begingroup\$ the coreutils yes takes an optional argument on the command line, not stdin. \$\endgroup\$ – Brian Minton Dec 18 '14 at 20:05
  • 8
    \$\begingroup\$ @MrLore: to pipe into other programs that might constantly ask for confirmation of the various things they are doing, so you dont have to sit there typing the ys yourself. \$\endgroup\$ – marcus erronius Dec 20 '14 at 16:57

76 Answers 76

1 2
3
0
\$\begingroup\$

Python 3, 40 bytes - 10 = 30

i=input()
while 1:print(i or chr(11**2))

Stops with SIGINT (Ctrl+C)

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

R, 27-10 = 17 bytes

x=scan(,'');repeat print(x)

Repeats stdin over and over and over. No y or \n in the code.

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

OIL, 23 bytes noncompeting

120
8

17

1

4

11
6
6

Increments 120, transforms it to a "y", and repeatedly print it and a newline.

Can be stopped with SIGINT / Ctrl+C.

Variant that receives something from stdin instead:

OIL, 13-10 = 3 bytes

5

4

11
6
2
\$\endgroup\$
0
\$\begingroup\$

AHK, 38 bytes - 10 = 28

c=%1%
Loop,3
FileAppend,% c Chr(9+1),*
  • I may have misunderstood, but I think the 9+1 is allowed. If not, I'll delete this.
  • If Loop is used without brackets {}, it is assumed that only the next line is part of the loop.
  • If Loop is used without a count, it will repeat until it encounters a Break.
  • Per the help file, if an asterisk is passed as the filename into FileAppend, it outputs to stdout.
  • %1% is the first passed argument.
  • If I could use the input variable %1% directly, that would have been nice. It gets funny with the Chr function following it, though, and would have ended up being more bites.
\$\endgroup\$
0
\$\begingroup\$

TI-BASIC, 8 bytes

While not(getKey:Disp Str0:End
\$\endgroup\$
0
\$\begingroup\$

Chip, 15+3 = 18 bytes

+3 for flag -w, which ignores stdin and generates an endless stream of null bytes for input instead.

dbae
*}++g
 `zf

Try it online!
Note about the TIO: instead of using the -w flag, it uses input. The behavior is the same, except that input is finite. One byte of output per byte of input.

Explanation

*}+
 `z

This is a 1-clock. On the first byte, the + is active, and on the second byte, the other three elements are active. The two states alternate endlessly.

d ae
* ++g
   f

This produces the output corresponding to 'y'. These letters each map to a bit: _gfed__a -> 0b01111001 -> 0x79 -> 'y'.

db
*}
 `z

This produces the output corresponding to '\n'. Similar to above, but we have these bits: ____d_b_ -> 0b00001010 -> 0x0a -> '\n'.

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

Tcl, 39 bytes

while 1 {puts [format %c [expr 11*11]]}

Try it online!

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

><>, 5 bytes - 10 = -5

a{:o!

Try it online!

Takes input via the -s flag and repeats it forever until the program is interrupted.

\$\endgroup\$
2
  • \$\begingroup\$ To qualify for the bonus, I think this should print y if no input is given. \$\endgroup\$ – user41805 Apr 15 '18 at 12:42
  • \$\begingroup\$ @Cowsquack Nope, it's optional \$\endgroup\$ – Jo King Apr 15 '18 at 13:13
0
\$\begingroup\$

MathGolf, 8 bytes -10 = -2

╜É'x)Äo∟

Try it online!

Explanation

╜É         else without if (executes block of 3 chars if input is empty)
  'x)      increment "x" to get "y"
     Ä     start block of length 1
      o    print TOS without popping
       ∟   do while true without popping

Quite straightforward, but gets the job done.

\$\endgroup\$
3
  • \$\begingroup\$ You don't need to print y if there's no input \$\endgroup\$ – Jo King Sep 20 '19 at 22:12
  • \$\begingroup\$ maxb bug report for MathGolf: string input which contains spaces are automatically transformed to lists (even if they are already inside lists). I noticed this, because it screwed up the length builtins £ and h: Try it online. \$\endgroup\$ – Kevin Cruijssen Oct 8 '19 at 12:10
  • \$\begingroup\$ @KevinCruijssen I think I overcomplicated the implementation of implicit input, which causes these errors. I'll see if I can switch to a pure ast.literal_eval statement for the input, which should solve a lot of issues. \$\endgroup\$ – maxb Oct 10 '19 at 10:35
0
\$\begingroup\$

Python 3, 25 bytes

while 1:print(chr(11*11))

Try it online!

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

Microscript, 19-10=9

If['w2s]1{CaI#!Zx}h

Besides the initial input, takes an extra input after each iteration and halts when and if any of the additional inputs is non-empty (the specs for the question said to provide a way to make it halt).

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

Python 3, 26-10=16 bytes

x=input()
while 1:print(x)

Seems too easy. Never posted here before so probably done something dumb. What did I miss?

UPDATE I don't think this is cheating (35-10=25):

exec("x=input()\nwhile 1:print(x)")
\$\endgroup\$
6
  • \$\begingroup\$ Welcome to the site! I think this question doesn't allow newlines so you should use ; instead. The question is a little confusing IMO and I've been here a while. \$\endgroup\$ – Wheat Wizard Dec 23 '19 at 16:57
  • \$\begingroup\$ Does it satisfy constraint about closing program? \$\endgroup\$ – Qwertiy Dec 23 '19 at 16:59
  • \$\begingroup\$ @WheatWizard, python doesn't support semicolons. \$\endgroup\$ – Qwertiy Dec 23 '19 at 17:00
  • \$\begingroup\$ Oh right. So I'm not even allowed newlines in my source. Got it! :laugh: \$\endgroup\$ – Andrew Parker Dec 23 '19 at 17:08
  • \$\begingroup\$ Best I can get with the bonus is 27, and I need the walrus to do that: x,*_=iter(lambda:print(z),z:=input()) \$\endgroup\$ – Andrew Parker Dec 23 '19 at 18:10
0
\$\begingroup\$

Mumps (M), 12 bytes - 10 = 2 bytes

Quite simple in Mumps (M), as the language allows single character commands and a newline is denoted by '!'...

R I F  W I,!

Breakdown:

Read STDIN into I, For (with two spaces) = infinite loop, Write contents of I, then newline (!)

Works on both InterSystems & YottaDB (GT.M).

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

Kotlin, 23 - 10 = 13 bytes

{while(1>0)println(it)}

Try it online!

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

C (gcc), 30 - 10 = 20 bytes

f(a){for(gets(&a);puts(&a););}

Try it online!

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

Pxem, filename: 22 bytes + content: 4 bytes - bonus: 10 bytes = 16 bytes.

  • Filename (escaped unprintables): .w.i.c\001.+.a\013.+l.w.el.a
  • Content: .v.p

Try it online!

With comments

XX.z
# while size==0 || pop!=0; do
.a.wXX.z
  # push getchar; dup; push 1; push pop+pop;
  # NOTE EOF is -1
  .a.i.c\001.+XX.z
# done
.a.aXX.z
# push 11; push pop+pop;
.a\013.+XX.z
# push whatever not zero; while pop!=0; do call content; push whatever not zero; done
.al.w.el.a
XX.z
# get called, with my stack initialized to original one
# reverse; pop all to putchar for each of them
# implicitly return, pushing each content of
# my stack from bottom to pop to original one
.a.v.p

Pxem, filename: 14 bytes + content: 0 bytes = 14 bytes.

  • Filename: (unprintables are escaped) .w\005\005.+\001x.+.p.a
  • Content: empty.

Wow, shorter than above!

Try it online!

\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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