66
\$\begingroup\$

Write the shortest program that prints the sound my alarm clock makes, and stops after an inputted number of beeps.

For reference, here is the sound my alarm makes:

beep beep beep beep beep beepbeep beepbeep beepbeep beepbeep beepbeep beepbeepbeep beepbeepbeep beepbeepbeep beepbeepbeep beepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeep

Basically beep, beepbeep, beepbeepbeep, and beepbeepbeepbeep repeated 5 times each with spaces in between, followed by a beepbeep...beep which is 25 beeps long with no spaces in between (does beep still sound like a word to you?).

Your program should take a number as input (assume it's between 0 and 75), and stop printing after that many beeps.

Note: Your program should stop after that many beeps, not after that many groups of beeps. For example, 7 will return beep beep beep beep beep beepbeep.

Whitespace in between beeps must follow the exact pattern above, although any trailing whitespace or unsuppressable output from your compiler or interpreter is allowed.

Test cases:

3   beep beep beep
0   
1   beep
7   beep beep beep beep beep beepbeep
8   beep beep beep beep beep beepbeep beep
55  beep beep beep beep beep beepbeep beepbeep beepbeep beepbeep beepbeep beepbeepbeep beepbeepbeep beepbeepbeep beepbeepbeep beepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeepbeep
67  beep beep beep beep beep beepbeep beepbeep beepbeep beepbeep beepbeep beepbeepbeep beepbeepbeep beepbeepbeep beepbeepbeep beepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeep

This is code golf, so the shortest answer in bytes, per language, wins.

\$\endgroup\$
16
  • 7
    \$\begingroup\$ This challenge is evil - will upvote if I didn't reach my daily vote limit. Care to continue showcasing dotcomma? \$\endgroup\$
    – null
    Aug 21, 2020 at 13:34
  • \$\begingroup\$ @HighlyRadioactive Thanks! This challenge might actually be doable in dotcomma, I think. \$\endgroup\$ Aug 21, 2020 at 13:41
  • \$\begingroup\$ Obviously, but it won't be short in any way. (BTW You don't have to provide a full program, so , or . is acceptable for length 1.) Where's the full spec for it and why isn't it TC? \$\endgroup\$
    – null
    Aug 21, 2020 at 13:43
  • 2
    \$\begingroup\$ @Kraay89 after the 5 groups of 4 beeps, it's 25 beeps in a row. The input is guaranteed to not exceed that many beeps. \$\endgroup\$ Aug 21, 2020 at 14:01
  • 1
    \$\begingroup\$ HNQ #1! Hooray! \$\endgroup\$
    – null
    Aug 22, 2020 at 9:31

33 Answers 33

29
\$\begingroup\$

JavaScript (ES7),  55  54 bytes

f=n=>n?f(n-1)+'beep'+[" "[n>50|n%~~(n**=.4)^52%~n]]:''

Try it online!

How?

Given \$1\le n< 50\$, we want to know the number of consecutive beeps that are expected in this part of the sequence. The exact value is given by:

$$\left\lfloor\sqrt{\frac{2n}{5}}+\frac{1}{2}\right\rfloor$$

which is a slightly modified version of A002024.

But in practice, we only need an exact value on the boundaries of the runs of beeps and we can deal with a few off-by-one errors. That's why we instead compute the following approximation:

$$k=\left\lfloor n^{2/5}\right\rfloor$$

We need to insert a space whenever one of the following conditions is satisfied:

  • \$k=1\$ and \$n\bmod 1=0\$ (the 2nd part being always true)
  • \$k=2\$ and \$n\bmod 2=1\$
  • \$k=3\$ and \$n\bmod 3=0\$
  • \$k=4\$ and \$n\bmod 4=2\$

All the above conditions can be merged into:

$$(n \bmod k) = (52 \bmod (k+1))$$

\$52\$ being the smallest integer \$x>0\$ such that \$x\bmod 3=1\$, \$x\bmod 4=0\$ and \$x\bmod 5=2\$.

We need an additional test for \$n\ge50\$, where all remaining beeps are concatenated together. Otherwise, unwanted spaces would be inserted, starting at \$n=54\$.

Hence the final JS expression:

n > 50 | n % ~~(n **= 0.4) ^ 52 % ~n

which evaluates to 0 when a space must be inserted.


JavaScript (ES7), 55 bytes

A simpler approach using a lookup bit mask.

f=n=>n?f(--n)+'beep'+(0x222222492555F/2**n&1?' ':''):''

Try it online!

\$\endgroup\$
17
\$\begingroup\$

x86-16 machine code, IBM PC DOS, 58 54 53 bytes

Binary:

00000000: a182 0086 e02d 3030 d50a 7423 95b8 2009  .....-00..t#.. .
00000010: b305 b101 8bf1 ba30 01cd 2183 fe05 740c  .......0..!...t.
00000020: e20a 4b75 03b3 0546 8bce cd29 4d75 eac3  ..Ku...F...)Mu..
00000030: 6265 6570 24                             beep$

Listing:

A1 0082         MOV  AX, WORD PTR [82H] ; command line AL = first char, AH = second char 
86 E0           XCHG AH, AL             ; endian convert 
2D 3030         SUB  AX, '00'           ; ASCII convert 
D5 0A           AAD                     ; BCD to binary convert 
74 23           JZ   EXIT               ; handle 0 input case 
95              XCHG AX, BP             ; Beeps Counter (BP) = user input 
B8 0920         MOV  AX, 0920H          ; AH = 9, AL = ' ' 
B3 05           MOV  BL, 5              ; Space Counter (SC) = 5 
B1 01           MOV  CL, 1              ; Beeps per Space Counter (BpSC) = 1  
8B F1           MOV  SI, CX             ; Beeps per Space (BpS) = 1 
BA 0130         MOV  DX, OFFSET BEEP    ; DX pointer to 'beep' string 
            BEEP_LOOP: 
CD 21           INT  21H                ; display beep
83 FE 05        CMP  SI, 5              ; exceeded 50 beeps? 
74 0C           JZ   NO_SPACE           ; if so, don't display space
E2 0A           LOOP NO_SPACE           ; if BpSC not zero, don't display space 
4B              DEC  BX                 ; decrement Space Counter (SC) 
75 03           JNZ  DO_SPACE           ; if SC is zero, restart it and increment BpS 
B3 05           MOV  BL, 5              ; reset SC to 5 
46              INC  SI                 ; increment BpS 
            DO_SPACE: 
8B CE           MOV  CX, SI             ; reset Beeps per Space Counter (BpSC) 
CD 29           INT  29H                ; display space 
            NO_SPACE: 
4D              DEC  BP                 ; decrement Beeps Counter (BP) 
75 EA           JNZ  BEEP_LOOP 
            EXIT: 
C3              RET                     ; return to DOS 

        BEEP    DB   'beep$'

Someone in the comments described this challenge as "evil". I wouldn't go that far... but definitely lacking empathy.

Arbitrary modulos can be pesky in x86 when registers are tight. This is the inelegant counter/countdown approach (seemed only appropriate for an alarm clock challenge), basically just jockeying these three counters:

  • SI = Beeps per Space (BpS): Start at 1. Increment every 5 spaces displayed. Once 5 is reached, no more spaces are displayed.
  • BX = Space counter (SC): Start at 5. Decrement every space displayed. At 0, increment BpS and reset to 5.
  • CX = Beeps per Space Counter (BpSC): Start at 1. Decrement every 'beep' displayed. At 0, display a space and reset to current BpS.

A standalone PC DOS executable, input is via command line.

enter image description here

Props: -1 byte thx to @gastropner!

\$\endgroup\$
4
  • \$\begingroup\$ I can only get it to work if I give it two digits on the command line. (Possibly newline as second char of command line otherwise?) That said, you can shave a byte off if you replace DEC CX / JNZ NO_SPACE with LOOPNZ NO_SPACE. \$\endgroup\$
    – gastropner
    Aug 21, 2020 at 21:50
  • \$\begingroup\$ @gastropner thx -- great catch on that LOOP! Oh, I did that screenshot before I took out some of the input code to parse input... guess you'd need to zero pad single digits now... hmm... \$\endgroup\$
    – 640KB
    Aug 22, 2020 at 0:54
  • 4
    \$\begingroup\$ it could be made less empathic and more evil, replacing int 21h with some out 43h, al \$\endgroup\$
    – dlatikay
    Aug 22, 2020 at 8:04
  • \$\begingroup\$ @dlatikey yes! Should be bonus points if your answer actually makes the correct beep noises! \$\endgroup\$
    – 640KB
    Aug 22, 2020 at 13:04
11
\$\begingroup\$

Python 3, 134 124 118 bytes

def f(n):
 b=[*'beep']*n
 for i in b'\4\t\16\23\30!*3<ER_ly\x86\x97\xa8\xb9\xca\xdb':b.insert(i,' ')
 return''.join(b)

Try it online!

Explanation: Simply works by inserting a blank space at the required indices of the string.

Thanks to pxeger for -6 bytes

\$\endgroup\$
6
  • \$\begingroup\$ Nice! I'm sure there's some way to condense the 4,9,...,219. \$\endgroup\$ Aug 21, 2020 at 14:09
  • \$\begingroup\$ @RedwolfPrograms Yeah got -10 bytes so far. \$\endgroup\$ Aug 21, 2020 at 14:21
  • 3
    \$\begingroup\$ pxeger suggested a possible golf (they don't have enough rep to leave a comment) \$\endgroup\$ Aug 21, 2020 at 14:41
  • \$\begingroup\$ thanks, updated \$\endgroup\$ Aug 21, 2020 at 14:50
  • 2
    \$\begingroup\$ Realised you can also replace \x04\t\x0e\x13\x18 with \4\t\16\23\30 using octal escapes - @ovs gave me the idea. (also I have enough rep to comment now!) \$\endgroup\$
    – pxeger
    Aug 22, 2020 at 10:56
9
\$\begingroup\$

R, 95 bytes

substr(s<-Reduce(paste,strrep("beep",c(rep(1:4,e=5),25))),1,c(el(gregexpr("b",s))[scan()]+3,0))

Try it online!

Thanks to @Dingus for pointing out a bug which made my code longer (and also wrong). Thanks to madlaina for suggesting a better regex.

Outgolfed handily by Dominic van Essen.

nreps <- c(rep(1:4,e=5),	# repeat the beeps 1,2,3,4 each 5 times
		25)		# and 25 times
beep <- strrep("beep",nreps)	# build a list of the repeated "beep"s
s <- Reduce(paste,beep)		# combine into one string, separated by spaces
i <- el(gregexpr("b",s))	# find the start index of each occurrence of a "beep"
e <- i[scan()]+3		# find the end index: the starting point of the n'th beep + 3
substr(s,1,c(e,0))		# and substring into s from 1 to e (or 0 if e is empty)
\$\endgroup\$
13
  • 3
    \$\begingroup\$ The part I understand here is b="beep" :D \$\endgroup\$ Aug 21, 2020 at 14:13
  • 1
    \$\begingroup\$ @RedwolfPrograms I'm adding an explanation :-) \$\endgroup\$
    – Giuseppe
    Aug 21, 2020 at 14:13
  • \$\begingroup\$ @RedwolfPrograms added! although I did also encounter some more golfs, so now it's harder to understand... \$\endgroup\$
    – Giuseppe
    Aug 21, 2020 at 14:22
  • 1
    \$\begingroup\$ I don't know R, but I think you can get rid of the b variable and use "beep" directly in the strrep and use just "b" as the regex for trimming. \$\endgroup\$
    – madlaina
    Aug 21, 2020 at 14:44
  • \$\begingroup\$ This appears to finish with a group of 5 beeps with input 20, and never produces a group of 3 beeps... \$\endgroup\$ Aug 21, 2020 at 14:57
8
\$\begingroup\$

R, 89 87 73 69 bytes

Edit: -20 (yes, 20) bytes thanks to Giuseppe

x=scan();cat(strrep("beep",c(b<-(a=5:24%/%5)[cumsum(a)<x],x-sum(b))))

Try it online!

The guts of this are stolen from Giuseppe's R answer, so please upvote that one... Edit: especially after he's now massively golfed-down this one!

However, I wanted to see whether a simpler, non-regex approach of constructing the correct number of 'beep'-repetitions (instead of making a very long one and then cutting it down) could be shorter.

So far it is...

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Very clever! And of course I don't mind you borrowing that, it's something I probably would have commented as shorter if I hadn't posted first :-) Plus, I don't think that paste does much of anything! \$\endgroup\$
    – Giuseppe
    Aug 21, 2020 at 15:32
  • 2
    \$\begingroup\$ 73 bytes: since the condition is always cumsum(a)<x, and x<=75, then you can just drop the 25. \$\endgroup\$
    – Giuseppe
    Aug 21, 2020 at 15:44
  • 1
    \$\begingroup\$ aaand 69 bytes by replacing the rep \$\endgroup\$
    – Giuseppe
    Aug 21, 2020 at 15:52
  • \$\begingroup\$ Brilliant! That's real golfing! Why don't I ever realise these input-condition tricks myself? Thanks! \$\endgroup\$ Aug 21, 2020 at 15:54
  • \$\begingroup\$ ehh, I was so fixated on the regex after seeing you use gregexpr somewhere recently. That integer division golf is one of the many Tips for golfing in R :-) \$\endgroup\$
    – Giuseppe
    Aug 21, 2020 at 16:04
8
\$\begingroup\$

Python 2, 59 bytes

f=lambda n:n*"?"and f(n-1)+"beep "[:4|0x444444924aabe>>n&1]

Try it online!

Uses a hardcoded lookup table to decide whether to put a space after each beep. I attempted to make formulas instead but didn't find something shorter.

\$\endgroup\$
8
\$\begingroup\$

dotcomma (old), 96577 bytes

Dotcomma is a language I created. I don't think I have any documentation or interpreter to link to yet, so it's not really competing at the moment. Takes input based on the number of inputs. Because it's so long, and very repetitive, here's the two blocks it's made up of:

For every beep without a space:

[[,.],[[[.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.].,][[.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.].,][[.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.].,][[.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.].,]]]

For every beep with a space:

[[,.],[[[.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.].,][[.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.].,][[.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.].,][[.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.].,][[.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.][.].,]]]

There's probably a way to golf down those constants. Anyway, I'll post an explanation when I have time.

Update: Dotcomma now has documentation and an interpreter. Because I added so many important new features since I posted this, it's practically a different language. If anyone else wants to post a different dotcomma answer that uses the full extent of the language's features, go ahead!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ The most impressive answer I've ever seen. \$\endgroup\$
    – null
    Aug 24, 2020 at 13:47
  • 1
    \$\begingroup\$ I would have called the language "ManySquareB00bs"... \$\endgroup\$
    – jmizv
    Aug 26, 2020 at 6:36
7
\$\begingroup\$

Husk, 16 15 bytes

wmΣC:Ṙ5ḣ4¹R¨⁸ep

Try it online!

Explanation

¨⁸ep    Compressed string literal "beep"
R       Repeat n times, n is input:
          ["beep","beep",..,"beep"]

C:Ṙ5ḣ4¹    Cut the above into pieces.
    ḣ4     Range to 4: [1,2,3,4]
  Ṙ5       Replicate 5 times:
             [1,1,1,1,1,2,2,2,2,2,..,4]
 :    ¹    Append n:
             [1,1,1,1,1,2,2,2,2,2,..,4,n]
C          Cut the beep list to these lengths:
             [["beep"],["beep"],..,[..,"beep","beep"]]
           C stops when it runs out of elements, possibly cutting the last list short.
           In this case it has to, since the beep list has length n.

mΣ    Concatenate each:
        ["beep","beep",..,"beepbeep...beep"]
w     Join by spaces, implicitly print.
\$\endgroup\$
7
\$\begingroup\$

Poetic, 853 bytes

anything i did was futile
o,a clock i set definitely failed
i know i,at one A.M,crave a rest
i notice,o!the alarm!it beeps
it provides no break to get a dream
its six A.M
aaggh,i got up
should i get sleep at six A.M while in bed?nope,never
i need to snooze now,but couldnt
im tired
ill get cereal:a bowl,milk,flakes
o no,the milk spills
dammit,i shout,getting kleenex and old unclean napkins
next,the pouch of frosted flakes
finally,i make a toast
i look,o no!eight A.M
must i hustle,so i begin at ten?i needed to rush,i am tardy
so i change:i get a jacket,i get a shirt
aw hell,o no,found no pair o pants
ill clearly suffer in a pair o boxers
i see,o no!eleven A.M
its a shame,o,too late
really,ill wear a blouse
so now i hurry
o,here now
i sit
time flies
i see,o my!three P.M
now i earned a rest
i badly ne-ee-ee-ee-eeeded a nap
i topple,and then i do

Try it online!

This was a tough program to write. I wrote the poem about an alarm that wakes me up way too early.

\$\endgroup\$
6
\$\begingroup\$

Jelly,  17  16 bytes

4Rx5Ä‘œṖȧ€“&?»$K

A monadic Link accepting an integer which yields a list of characters.

Try it online!

How?

4Rx5Ä‘œṖȧ€“&?»$K - Link: integer, n     e.g. 8
4                - four                      4
 R               - range                     [1,2,3,4]
   5             - five                      5
  x              - times                     [1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4]
    Ä            - accumulate                [1,2,3,4,5,7,9,11,13,15,18,21,24,27,30,34,38,42,46,50]
     ‘           - increment                 [2,3,4,5,6,8,10,12,14,16,19,22,25,28,31,35,39,43,47,51]
              $  - last two links as a monad - i.e. f(n):
          “&?»   -   compressed string       "beep"
        ȧ€       -   (n) AND each ("beep")   ["beep","beep","beep","beep","beep","beep","beep","beep"]
      œṖ         - split before indices      [["beep"],["beep"],["beep"],["beep"],["beep"],["beep","beep"],["beep"]]
               K - join with spaces          "beep beep beep beep beep beepbeep beep"
\$\endgroup\$
6
\$\begingroup\$

05AB1E, 22 21 20 19 bytes

F’¼®b’4L5и{¦.¥NåúRJ

-1 byte by porting the approach used in multiple other answers.

Try it online or verify all test cases.


Original approach:

05AB1E (legacy), 22 21 bytes

'¬ž4L₂¸«×5иé»Z¡I£'p«J

Output is joined by newlines. If this has to be spaces instead, 1 byte has to be added by replacing the » with ðý.

Try it online or verify all test cases.

I wanted to use the legacy version of 05AB1E, since the maximum builtin Z works on strings (getting the character with the largest codepoint), which isn't the case in the new version of 05AB1E. This would have saved a byte over 'r. Unfortunately, the legacy version lacks the append_to_list builtin ª, so we'll have to use ¸« instead.

So here is a regular 05AB1E version as well with the same 22 21 bytes:

'¬ž4L₂ª×5иé»'r¡I£'p«J

Try it online or verify all test cases.

Explanation:

F                       # Loop `N` in the range [0, (implicit) input-integer):
 ’¼®b’                  #  Push dictionary string "peeb"
 4L                     #  Push list [1,2,3,4]
   5и                   #  Repeat it 5 times: [1,2,3,4,1,2,3,4,...]
     {                  #  Sort it: [1,1,1,1,1,2,2,2,2,2,...]
      ¦                 #  Remove the first value
       .¥               #  Undelta this list (with implicit leading 0):
                        #   [0,1,2,3,4,6,8,10,12,14,17,20,23,26,29,33,37,41,45,49]
         Nå             #  Check if `N` is in this list (1 if truthy; 0 if falsey)
           ú            #  Pad "peeb" with that many leading spaces
            R           #  Reverse it to "beep" or "beep "
             J          #  Join all strings on the stack together
                        # (after the loop, the result is output implicitly)

'¬ž                    '# Push dictionary string "beer"
   4L                   # Push a list [1,2,3,4]
     ₂                  # Push 26
      ª                 # New version: Append it as trailing item to the list
      ¸«                # Legacy version: Wrap into a list; merge the lists together
                        #  [1,2,3,4,26]
        ×               # Repeat each string that many times:
                        #  ["beer","beerbeer","beerbeerbeer","beerbeerbeerbeer",...]
         5и             # Repeat this list five times
           é            # Sort it based on length
            »           # Join all strings in the list by newlines
             'r        '# New version: Push "r"
             Z          # Legacy version: Push the maximum character (without popping),
                        # which is "r"
               ¡        # Split the string on "r"
                I£      # Leave the first input amount of substrings
                  'p«  '# Append a "p" to each string in the list
                     J  # And join it all together again
                        # (after which the result is output implicitly)

See this 05AB1E tip of mine (section How to use the dictionary?) to understand why ’¼®b’ is "peeb" and '¬ž is "beer".

\$\endgroup\$
2
  • 2
    \$\begingroup\$ While not particularly useful, an alarm that repeatedly yells beer might be funny \$\endgroup\$ Aug 21, 2020 at 19:23
  • 1
    \$\begingroup\$ @RedwolfPrograms :) Tbh, I just used the first word in the dictionary of bee*, since beep isn't in the 05AB1E dictionary (which is why my 20-byte answer uses bee + p). I could also have used been or beef instead of beer, but beer is indeed the most comical to use. ;) \$\endgroup\$ Aug 21, 2020 at 21:31
5
\$\begingroup\$

Io, 81 78 75 bytes

Shamelessly ported from Arnauld's answer.

f :=method(i,if(i>0,f(i-1).."beep".." "repeated(1200959982447294>>i&1),""))

Try it online!

Io, 152 113 bytes

Port of Manish Kundu's answer.

method(x,O :=("beep"repeated(x)asList);"	!*3<ER_ly†—¨¹ÊÛ"foreach(i,if(i<x*4,O atInsert(i," ")));O join)

Try it online!

Explanation

method(x,    // Input x
    O :=("beep"repeated(x)asList)            // "beep" repeated x times
    "	!*3<ER_ly†—"foreach(i,           // For every codepoint in this string:
        if(i<x*4,                            //     If doing this doesn't cause an error:
             O atInsert(i," ")));            //          Insert at this position
    O join)                                  // Join O without a separator
\$\endgroup\$
5
\$\begingroup\$

J, 50 46 bytes

;@({.<@,&' '@;/.[$<@'beep')&((##\)25,~1+5#i.4)

Try it online!

how

25,~1+5#i.4 produces:

1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 25

(##\) pairs that with an integer list of the same length:

1 1 1 1 1 2 2 2 2  2  3  3  3  3  3  4  4  4  4  4 25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

and duplicates the bottom list according to the corresponding element of the top list:

1 2 3 4 5 6 6 7 7 8 8 9 9 10 10 11 11 11 12 12 12 13 13 13 14 14 14 15 15 15 16 16 16 16 17 17 17 17 18 18 18 18 19 19 19 19 20 20 20 20 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21

Call this our "key". We're going to use it to group our "beep"s together.

Our key becomes the right argument to ;@({.<@,&' '@;/.[$<@'beep').

[$<@'beep' first duplicates "beep" according to the input. Say, for an input of 8 we get:

┌────┬────┬────┬────┬────┬────┬────┬────┐
│beep│beep│beep│beep│beep│beep│beep│beep│
└────┴────┴────┴────┴────┴────┴────┴────┘

{. takes the first 8 elements of our key, creating a new key:

1 2 3 4 5 6 6 7

The key adverb /. applies the verb <@,&' '@; to each group defined by the new key. It unboxes, appends a space, and reboxes:

┌─────┬─────┬─────┬─────┬─────┬─────────┬─────┐
│beep │beep │beep │beep │beep │beepbeep │beep │
└─────┴─────┴─────┴─────┴─────┴─────────┴─────┘

;@ unboxes again, giving the result:

beep beep beep beep beep beepbeep beep
\$\endgroup\$
5
\$\begingroup\$

Python 3, 108 120 118 110 bytes

+12 because I forgot to include the import statement which I had to put in the TIO header, not body

-2 by replacing \x00 with \0 - thanks to @ovs

-8 by filtering instead of replacing, and switching from . to !

import zlib;lambda x:filter(33 .__ne__,zlib.decompress(b'x\x9cKJM-PH\xc2A(\x92\xc7\xa26\x97nb4!\0hm{7')[:x*5])

Try it online!

How It Works

Sorry. I couldn't be bothered to come up with a clever algorithm.

zlib compressed string: beep beep beep beep beep beep!beep beep!beep beep!beep beep!beep beep!beep beep!beep!beep beep!beep!beep beep!beep!beep beep!beep!beep beep!beep!beep beep!beep!beep!beep beep!beep!beep!beep beep!beep!beep!beep beep!beep!beep!beep beep!beep!beep!beep beep!beep!beep!beep!beep!beep!beep!beep!beep!beep!beep!beep!beep!beep!beep!beep!beep!beep!beep!beep!beep

which is indexed into upto n*5th character, and then we filter for the bytes which don't equal 33 (exclamation mark). I chose ! using a brute-force to find the the shortest compressed output from zlib.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Try to avoid golfing with suggested edits. I'll leave a comment on the other answer pointing to your suggestion here. \$\endgroup\$ Aug 21, 2020 at 14:40
  • 1
    \$\begingroup\$ You can get this 2 bytes shorter by replacing \x00 with \0. \$\endgroup\$
    – ovs
    Aug 22, 2020 at 10:48
4
\$\begingroup\$

Wolfram Language (Mathematica), 188 bytes

If[#==0,"",c=#;T@v_:=v~Table~5;w=Accumulate[z=Flatten@{T/@Range@4,25}];StringRiffle[""<>#&/@Join[1~Table~#&/@z[[;;Max@Position[w,m=Max@Select[w,#<=c&]]]],{Table[1,c~Mod~m]}]/. 1->"beep"]]&

Try it online!

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

Python 3 , 164 Bytes

Try it online

This is my recursive approach. Somehow it's worse than I imagined it in my head.

Explanation: n is the input number

The g function generates the beep sequence, where x controls the numbers of "beep"s . Every 4th call x is incremented by 1, and with the 16th call it's set to 25. In the next call it's reset to 1 . g generates n groups of "beep"s the string is stored in v

f cuts v to the corrext number by searching for the next "b" in v until n is reached.

g=lambda s,x,m:s if m>n else g(s+"beep"*x+" ",([25,1]+[x+(m%4<1)]*14)[(m+1)%16],m+1)
v=g("",1,1)
f=lambda m,i:v[:i] if m>n else f(m+1,v.find("b",i+1))
print(f(1,1))
\$\endgroup\$
4
\$\begingroup\$

APL (Dyalog Unicode), 32 bytes

∊' ',⍨¨(⎕↑×∊↑⍨¨25,⍨5/⍳4)⊂⊂'beep'

Try it online!

With significant golfing input from ngn.

Explanation

The code inside the parens builds a boolean array describing the grouping pattern, which we'll come back to below; quad () prompts for the quantity of beeps and the pattern is cut to that number. To the right of the parens the word 'beep' is enclosed (monadic ) to make it a single thing (instead of an array of 4 characters), and that is partition-enclosed (dyadic ) by the pattern which groups the beep and implicitly repeats it to match the cut pattern length. To the left of the parens, the beeps get one space (' ') appended (,⍨) to each (¨) group of them, then get flattened () into the result string.

Building the group pattern follows this progression:

      5/⍳4           ⍝ five-replicate the first four numbers

1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4


      25,⍨5/⍳4       ⍝ append 25 for the long run

1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 25


      ↑⍨¨25,⍨5/⍳4    ⍝ turn each (¨) of the numbers into 
                     ⍝ a group that long, padded with zeros.
                     ⍝ using take selfie (↑⍨).
                     ⍝ e.g. Take first 3 items out of "3", get 3 0 0.
┌→┐ ┌→┐ ┌→┐ ┌→┐ ┌→┐ ┌→──┐ ┌→──┐ ┌→──┐ ┌→──┐ ┌→──┐ ┌→────┐ ┌→────┐ ┌→────┐ ┌→────┐ ┌→────┐ ┌→──────┐ ┌→──────┐ ┌→──────┐ ┌→──────┐ ┌→──────┐ ┌→─────────────────────────────────────────────────┐
│1│ │1│ │1│ │1│ │1│ │2 0│ │2 0│ │2 0│ │2 0│ │2 0│ │3 0 0│ │3 0 0│ │3 0 0│ │3 0 0│ │3 0 0│ │4 0 0 0│ │4 0 0 0│ │4 0 0 0│ │4 0 0 0│ │4 0 0 0│ │25 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0│
└~┘ └~┘ └~┘ └~┘ └~┘ └~──┘ └~──┘ └~──┘ └~──┘ └~──┘ └~────┘ └~────┘ └~────┘ └~────┘ └~────┘ └~──────┘ └~──────┘ └~──────┘ └~──────┘ └~──────┘ └~─────────────────────────────────────────────────┘


      ∊↑⍨¨25,⍨5/⍳4    ⍝ flatten (∊) the nesting

1 1 1 1 1 2 0 2 0 2 0 2 0 2 0 3 0 0 3 0 0 3 0 0 3 0 0 3 0 0 4 0 0 0 4 0 0 0 4 0 0 0 4 0 0 0 4 0 0 0 25 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0


      ×∊↑⍨¨25,⍨5/⍳4    ⍝ use direction (×) to turn all non-zero into 1
                       ⍝ 1 marks the start of each group, 0 pads their length.
                       ⍝ A boolean group-array for the full beep pattern

1 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0


      20↑×∊↑⍨¨25,⍨5/⍳4    ⍝ take (↑) 20 beeps. (⎕ number beeps)
                          ⍝ This is how it cuts in the middle of a 
                          ⍝ run of beepbeepbeep, by cutting the pattern.

1 1 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 0 1 0


      (20↑×∊↑⍨¨25,⍨5/⍳4)⊂⊂'beep'    ⍝ p-enclose of 'beep' shows the grouping,
                                    ⍝ and the cutoff group at the end.

┌→───────┐ ┌→───────┐ ┌→───────┐ ┌→───────┐ ┌→───────┐ ┌→──────────────┐ ┌→──────────────┐ ┌→──────────────┐ ┌→──────────────┐ ┌→──────────────┐ ┌→─────────────────────┐ ┌→──────────────┐
│ ┌→───┐ │ │ ┌→───┐ │ │ ┌→───┐ │ │ ┌→───┐ │ │ ┌→───┐ │ │ ┌→───┐ ┌→───┐ │ │ ┌→───┐ ┌→───┐ │ │ ┌→───┐ ┌→───┐ │ │ ┌→───┐ ┌→───┐ │ │ ┌→───┐ ┌→───┐ │ │ ┌→───┐ ┌→───┐ ┌→───┐ │ │ ┌→───┐ ┌→───┐ │
│ │beep│ │ │ │beep│ │ │ │beep│ │ │ │beep│ │ │ │beep│ │ │ │beep│ │beep│ │ │ │beep│ │beep│ │ │ │beep│ │beep│ │ │ │beep│ │beep│ │ │ │beep│ │beep│ │ │ │beep│ │beep│ │beep│ │ │ │beep│ │beep│ │
│ └────┘ │ │ └────┘ │ │ └────┘ │ │ └────┘ │ │ └────┘ │ │ └────┘ └────┘ │ │ └────┘ └────┘ │ │ └────┘ └────┘ │ │ └────┘ └────┘ │ │ └────┘ └────┘ │ │ └────┘ └────┘ └────┘ │ │ └────┘ └────┘ │
└∊───────┘ └∊───────┘ └∊───────┘ └∊───────┘ └∊───────┘ └∊──────────────┘ └∊──────────────┘ └∊──────────────┘ └∊──────────────┘ └∊──────────────┘ └∊─────────────────────┘ └∊──────────────┘


      ∊' ',⍨¨(20↑×∊↑⍨¨25,⍨5/⍳4)⊂⊂'beep'    ⍝ append one space in each group
                                           ⍝ and flatten.

beep beep beep beep beep beepbeep beepbeep beepbeep beepbeep beepbeep beepbeepbeep beepbeep
\$\endgroup\$
3
\$\begingroup\$

Rust, 76 127 bytes

|n|(0..n).fold("".into(),|a,i|a+"beep"+&" "[..0x222222492555F>>i.min(63)&1])

Try it online!

Shamelessly ported from Arnauld's JS solution

The binary constant has a bit set wherever the "beep" should be followed by a space.

Old solution

|n|[1,2,3,4,25].iter().fold(format!(""),|a,&i|a+&("beep".repeat(i)+" ").repeat(5)).rsplitn(176-n,'b').last().map(str::to_owned)

Try it online!

Explanation:

We first construct the string beep beep beep beep beep beepbeep beepbeep beepbeep beepbeep beepbeep beepbeepbeep... with the last 25 successive beeps also being repeated 5 times. This string contains 175 beeps, so we trim from the right to and including the 176-nth b and take the sub string left of there.

\$\endgroup\$
1
  • \$\begingroup\$ A fun byte save: a+&" beep"[!0x444444924aabe>>i.min(63)&1..] \$\endgroup\$
    – Lynn
    Feb 22 at 22:08
3
\$\begingroup\$

APL+WIN, 50 bytes

Prompts for input of n:

(4×(p/m),¯1↑-(p←n≤0)/n←(+\m←(5/⍳4),25)-⎕)⍴¨⊂'beep'

Try it online! Courtesy of Dyalog Classic

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

Charcoal, 23 bytes

FN«beep¿&⍘(XsB!,zOγX²ι→

Try it online! Link is to verbose version of code. Uses the popular bitmask approach. Explanation:

FN«

Loop the given number of times.

beep

Print a beep.

¿&⍘(XsB!,zOγX²ι→

If the appropriate bit in the constant is set, then move right one character. The constant is probably the same as everyone else's but here I'm effectively encoding it using base 95.

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

PowerShell, 68 bytes

(0.."$args"|%{' '*((0x444444924AABE-shr$_)%2)*($_-lt52)})-join'beep'

Try it online!

The script:

  1. generates an array of whitspace and empty strings
  2. joins array elements with 'beep'

The script can add a trailing whitespace that is allowed by the author. See the test cases in the TIO link.

The Powershell works with 64 bitmasks only, so I had to add a condition ($_-lt52)

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

C (gcc), 72 \$\cdots\$ 66 60 bytes

Saved 10 bytes thanks to the man himself Arnauld!!!

f(n){n&&printf(" beep"-~-(0x888889249557c>>n&n<55),f(n-1));}

Try it online!

Recursively calls itself \$n\$ times evaluating a bitwise expression (where the \$1\$ bits of a hard coded integer indicate if a space is needed) to determine whether or not to prefix the current beep with a space. This is done by adding \$0\$ or \$1\$ to a string literal (char* pointer) to offset it by one or not.

\$\endgroup\$
2
  • \$\begingroup\$ Your 1st approach is actually easier to golf: 60 bytes by going recursive. \$\endgroup\$
    – Arnauld
    Aug 22, 2020 at 14:02
  • \$\begingroup\$ @Arnauld Gave you the save from the 1st approach. Recursion rocks - thanks! :D \$\endgroup\$
    – Noodle9
    Aug 22, 2020 at 14:13
3
\$\begingroup\$

Vyxal, 20 bytes

4ɾƛS5*fvI;?Jf«⟇½⁰«*Ṅ

Try it Online!

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

PowerShell 5.1, 227 bytes

$n=10;function f($x){$r.Length-in$x};$c=0;$r="";$t=0;while($c-lt$n){$s=-1;switch($true){{f(0..24)}{$s=1}{f(25..69)}{$s=2}{f(70..134)}{$s=3}{f(135..219)}{$s=4}};$r+="beep";$t++;if($t-ne$s){$c++;continue};$r+=" ";$t=0;$c++};$r

Explanation: $n is the input number. I tried to write this without doing it via arrays because I felt like it would be cheating, as I had already read this answer. I used the length of the string to determine how many "beeps" were needed before I placed a space. If the length of the string is between 0 and 24, 1 space. If the length of the string is between 25 and 69, 2 spaces. etc.

Here's the "cleaner" version

$n = 9
function bl ($x) {$beepString.Length -in $x}
$count = 0
$beepString = ""
$beepsThisTime = 0
while($count -lt $n)
{
    $neededBeepsBeforeSpace = -1
    switch($true)
    {
        {bl(0..24)}{$neededBeepsBeforeSpace = 1}
        {bl(25..69)}{$neededBeepsBeforeSpace = 2}
        {bl(70..134)}{$neededBeepsBeforeSpace = 3}
        {bl(135..219)}{$neededBeepsBeforeSpace = 4}
    }

    $beepString += "beep"
    $beepsThisTime++
    if($beepsThisTime -ne $neededBeepsBeforeSpace){$count++;continue}
    $beepString+=" "
    $beepsThisTime = 0
    $count++
}
$beepString
\$\endgroup\$
2
\$\begingroup\$

Lua, 105 bytes

function b(n)t={}for i=5,24 do t[(i-1)*(i-2)//10]=' 'end for i=1,n do io.write('beep'..(t[i]or''))end end

Ungolfed code and test program:

function b(n)
    t={}
    for i=5, 24 do
        t[(i-1)*(i-2)//10] = ' '
    end
    for i=1, n do
        io.write('beep' .. (t[i] or ''))
    end
end

for k, v in ipairs({ 3, 0, 1, 7, 8, 55, 67, 75 }) do
    io.write(v .. '\t') b(v) print()
end

Output:

3   beep beep beep 
0   
1   beep 
7   beep beep beep beep beep beepbeep 
8   beep beep beep beep beep beepbeep beep
55  beep beep beep beep beep beepbeep beepbeep beepbeep beepbeep beepbeep beepbeepbeep beepbeepbeep beepbeepbeep beepbeepbeep beepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeepbeep
67  beep beep beep beep beep beepbeep beepbeep beepbeep beepbeep beepbeep beepbeepbeep beepbeepbeep beepbeepbeep beepbeepbeep beepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeep
75  beep beep beep beep beep beepbeep beepbeep beepbeep beepbeep beepbeep beepbeepbeep beepbeepbeep beepbeepbeep beepbeepbeep beepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeep beepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeepbeep

Try it online

Edit 1: Thank you for your suggestions :) It greatly helped to compress the sequence that was used.

Edit 2: 99 Bytes solution provided by Arnault, getting rid of the (-1) and using a partial and clever one's complement to decrease a number:

function b(n)t={}for i=4,23 do t[i*~-i//10]=' 'end for i=1,n do io.write('beep'..(t[i]or''))end end

Try it online

\$\endgroup\$
7
  • \$\begingroup\$ There's probably a way to golf the 1,2,3,...,50 some more, seems to follow a sort of regular pattern \$\endgroup\$ Aug 21, 2020 at 14:58
  • 1
    \$\begingroup\$ I tried some basic attempts which were longer than this sequence, still trying to figure something smaller \$\endgroup\$
    – Riptide
    Aug 21, 2020 at 15:01
  • 1
    \$\begingroup\$ You could map every integer to a character (for example starting at ASCII 32 (space)) and use a string to store the positions, which you extract the space positions from using string.byte(). Or skip the whole table t entirely and use string.find to check whether the position needs a space. \$\endgroup\$
    – madlaina
    Aug 21, 2020 at 15:36
  • 1
    \$\begingroup\$ There a few short formulas for the 1..50 sequence on OEIS \$\endgroup\$
    – zdimension
    Aug 22, 2020 at 14:23
  • \$\begingroup\$ You can save 2 bytes by using ~-i instead of (i-1). \$\endgroup\$
    – Arnauld
    Aug 23, 2020 at 15:58
2
\$\begingroup\$

Setanta, 146 144 140 124 123 bytes

-16 bytes by using a cheaper check for the first beep.
-1 byte by discovering that braces could be left out in one place.

gniomh(n){s:=""le i idir(0,n){a:=(i&freamh@mata((i-1)//5*8+1)+1)//2d:=(i-5*(a*a-a)/2)%a ma!d&i<51 s+=" "s+="beep"}toradh s}

Try it here!

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

Zsh, 64 bytes

repeat $1 z+=beep.
repeat 4 repeat x+=5,5 z[t+=x]=\ 
<<<${z//./}

Try it online!

Because of a bug that's not fixed in the Zsh version available on TIO, the x+=5,5 needs to be single-quoted for +2 bytes.

Works by constructing a string of beep., replacing some .s with spaces, and then removing the .s. The differences in the indeces at which to remove the .s are 5, 5, 5, 5, 5, 10, 10, ... 15, 15, 20, 20, 20, 20, 20, so we cumulatively sum these as we go.

  • repeat $1: do this {input} times:
    • z+=beep.: append beep. to the variable $z (which implicitly starts empty)
  • repeat 4:
    • repeat x+=5,5: do this 5 times, but also increment $x by 5 (it starts at 0 implicitly) before the loop starts:
      • t+=x: increment $t (the cumulative sum) by $x (again, $t starts at 0 implicitly)
      • z[]=\ : set the $tth character (1-indexed) of $z to a space
  • ${z//./}: take $z, with dots . replaced by {empty string}
  • <<<: print
\$\endgroup\$
2
\$\begingroup\$

dotcomma, 617 569 bytes

[,].[[[,.][[[[[.][.][.][.].,][,.].,][,.].,][[,][[[,][,.].,][,][,].,].,][,][[[,.][.][.].,][.][.][.].,][,][[[,.][[[,.][[,]].,][],].,],].[,][.,][[.][.][.][.][.].,].[.[.[[[,.][[].[],].,][[,][[,][[[[,][[[,],]].,],]].,][],][.[.[,].][,][,]].][,][[,][[,][,][,]].,][.[.[,].]][[,.][[].[],].,],.[[[,][[,.]].,][.[.[,].][,]]],.[.[.[,].][,[.][.][.][.][.].,][,]][,],.][.[.[,].][,]][[[,.][[].[],].,][,.].,].[.[,].][,][,],.][,][.[.[,].]][,[,[,[,[,[,[,[,.]]]]]]]]].,].[[.[.[,].]][[,.][[].[],].,][.[[,],]][.[.[,].][,][,]][.[,]][,.][.[.[,].][,][,.]][.[[,][,.],.]].][.[.[,.].]][,.]],.[[,.]]

-48 bytes by rearranging the last counter loop and thereby avoiding duplicate code.

Phew, I need to rearrange my brain again... Bracket chaos ^^

This is first try with this language. It's quite fun. The changes made to the old version seem to be very effective when I can shrink the program size to less than 1% of the old version. Are there plans to put this language on tio.run? What about plumber? I think that's interesing, too.

Use the following snippet on your own risk (especially when changing the dotcomma code. I had several freezes because I unintetionally created endless loops)

<script src="https://combinatronics.com/Radvylf/dotcomma/master/interpreter.js"></script><script src="https://code.jquery.com/jquery-3.5.1.min.js"></script><script>$(document).ready(function () {$("#btnInterpret").click(function () {$("#txtResult").text(interpret($("#txtCode").val(), parseInt($("#txtInput").val()), $("#lstOutputAs").children("option:selected").val()));});});</script><style>.textBox {background-color: white;border: 1px solid black;font-family: Courier New, Courier, monospace;width: 100%;}</style>Code: <textarea id="txtCode" type="text" class="textBox" style="height: 200px">[,].[[[,.][[[[[.][.][.][.].,][,.].,][,.].,][[,][[[,][,.].,][,][,].,].,][,][[[,.][.][.].,][.][.][.].,][,][[[,.][[[,.][[,]].,][],].,],].[,][.,][[.][.][.][.][.].,].[.[.[[[,.][[].[],].,][[,][[,][[[[,][[[,],]].,],]].,][],][.[.[,].][,][,]].][,][[,][[,][,][,]].,][.[.[,].]][[,.][[].[],].,],.[[[,][[,.]].,][.[.[,].][,]]],.[.[.[,].][,[.][.][.][.][.].,][,]][,],.][.[.[,].][,]][[[,.][[].[],].,][,.].,].[.[,].][,][,],.][,][.[.[,].]][,[,[,[,[,[,[,[,.]]]]]]]]].,].[[.[.[,].]][[,.][[].[],].,][.[[,],]][.[.[,].][,][,]][.[,]][,.][.[.[,].][,][,.]][.[[,][,.],.]].][.[.[,.].]][,.]],.[[,.]]</textarea><br />Input: <textarea id="txtInput" type="text" class="textBox">25</textarea><br /><input id="btnInterpret" type="button" value="Run" />Output as: <select id="lstOutputAs"><option value="true">String</option><option value="">Number array</option></select><br />Result:<br /><div id="txtResult" class="textBox" style="overflow-wrap: break-word"></div>

Code:

(sorry for the missing interpunctuation. This way I was able to run the commented code directly in the interpreter)

[,].[                           if input > 0
  [[,.][                        save input on recursion stack
    [
      [                         ### Build ASCII values for "b" "e" "p" and space " "
        [[.][.][.][.].,]        4
      [,.].,]                   8
    [,.].,]                     16
  
    [[,]                        put 16 on recursion stack and save copy in queue
      [
        [[,][,.].,]             32
      [,][,].,]                 96 
    .,]                         112 ("p")
                                in queue: 32 96 112
    [,]                         roll left (queue: "@p ")
    [[[,.][.][.].,][.][.][.].,] "b" "e"
    [,]                         roll left (queue: " bep")
    [[[,.][                     save " " on recursion stack
      [[,.][[,]].,]             reverse letters ("peb")
      [],                       insert 0 (start of queue)
    ].,],]                      save two copies of 32 (one for space character and one for counting beeps)
                                
                                ### Build list of "beep"s in reverse - each separated by a null char

                                Since the maximum input is 75 the 25 consecutive beeps at the end
                                will be limited by the input - so I can safely put a few more
                                beeps into the list because I just have a 32 handy

                                sc: spaces counter
                                pc: pattern counter
                                bc: beep counter

                                target queue: 0 0 sc pc bc " peb" 
                                current queue: "peb" 0 bc(32) " "

                                I will refer to the two consecutive 0s at the start of the variable
                                section as "start of queue" or just "start"

                                pc: |           1           |                   2                       |
                                sc: | 1 |  2 |  3 |  4 |  5 |    1   |    2   |    3   |    4   |    5  |
                                bc: | 1 |  1 |  1 |  1 |  1 |  1 | 2 |  1 | 2 |  1 | 2 |  1 | 2 | 1 | 2 |
                                    beep beep beep beep beep beepbeep beepbeep beepbeep beepbeep beepbeep 

    .[,]                        roll to start  
    [.,]                        insert 1 for spaces counter (since I append new beeps to the start of the
                                queue we are only one space away from the next pattern)
    [[.][.][.][.][.].,]         insert 5 for pattern counter
                                queue: pc(5) bc(32) " bep" 0 sc(1)

    .[                          while pattern counter > 0 ### Attention: current position is beep counter!
      .[                        while spaces counter > 0 ### Attention: current position is beep counter!
        .[                      while beep counter > 0
          [[,.][[].[],].,]      save beep counter -1 on queue (also hold it in recursion stack)

          [                     # put a copy of "beep" on the queue
            [,]                 roll over " "
            [[,][[              put "p" on recursion stack
              [[,][[            put "e" on recursion stack
                [,]             put "b" on recursion stack
                ,]]             put "b" on queue
              .,],]]            put "ee" on queue
            .,]                 put "p" on queue
            [],                 put 0 on queue
          ]
          [
            .[.[,].]            roll to start (roll until two consecutive 0s are found)
            [,][,]              go to beep counter
          ]
        .]                      return value for loop (end of beep counter loop)

                                # insert space
        [,]                     roll over beep counter
        [[,][[,][,][,]].,]      copy and insert space

                                # if spaces counter - 1 > 0: copy pattern counter to beep count
                                (the pattern counter contains the number of consecutive beeps
                                that should be separated by a space)

        [.[.[,].]]              roll to spaces counter
        [[,.][[].[],].,]        decrement spaces counter
        ,.[                     if spaces counter > 0
          [[,][[,.]].,]         replace beep counter with copy of pattern counter
          [.[.[,].][,]]         roll to pattern counter (if the spaces loop repeats we need to be at
                                the beep counter; I will read the next value to determine if the loop
                                should repeat; Thats's why I stop one value before beep counter)
        ],.[                    else                    
          .[.[,].]              roll to spaces count
          [,[.][.][.][.][.].,]  set it 5 for next round
          [,]                   roll to beep count
        ]
        [,],.                   get return value for loop (pattern counter for repeating 
                                or beep counter(0) for stopping)
      ]                         end of spaces loop

      [.[.[,].][,]]             roll to pattern counter
      [
        [[,.][[].[],].,]        decrement pattern counter
        [,.].,                  set new beep counter = pattern counter
      ]
      .[.[,].][,]               roll to pattern counter
      [,],.                     repeat if > 0
    ]                           end of pattern loop
    [,][.[.[,].]]               roll to start
    [,[,[,[,[,[,[,[,.]]]]]]]]   delete variables constants and excess space in front of first beep
  ].,]                          put input back into queue

                                ### Count beeps
                                The idea is to delete all 0s between the beeps until we match the input
                                then deleting everything behind it
                                I thought it would be easy to delete single 0s - What I didn't consider was
                                that I can end a loop only after a 0 has been processed so I needed a trick
                                What I do is: duplicate every character until I reach a 0 (which will also
                                be duplicated)
                                Then find the first 0 (reassigning it to the end of the queue) and delete
                                the second 0
                                After that I can delete the duplicated characters including the 0

  .[                            while input counter > 0
    [.[.[,].]]                  roll to start
    [[,.][[].[],].,]            decrement input counter
    [.[[,],]]                   duplicate until first 0
    [.[.[,].][,][,]]            roll to start + 2
    [.[,]]                      roll to first 0
    [,.]                        delete second 0
    [.[.[,].][,][,.]]           roll to start + 2 (deleting the second character)
    [.[[,][,.],.]]              delete every 2nd character until 0 found
  .]                            end of loop                     end of loop

  [.[.[,.].]]                   delete everything up to start
  [,.]                          delete input counter

],.[                            else (if input is 0)
  [,.]                          delete input and output nothing
]
\$\endgroup\$
2
  • 1
    \$\begingroup\$ This is incredible! Really cool to see someone else using one of my languages (and probably doing better at it than myself :p). Dennis, the owner of TIO, is inactive right now, but I'll look into getting Plumber/dotcomma added at some point. \$\endgroup\$ Oct 1, 2020 at 12:34
  • \$\begingroup\$ That's good to hear. I think, those languages will gain more attention, once they can be easily tested. I didn't try programming in Plumber yet, but I find it interesting to have multiple parallel execution paths. That way you can program in a "Game Of Life" style by timing and crashing into other packets \$\endgroup\$
    – Dorian
    Oct 2, 2020 at 7:19
2
\$\begingroup\$

Haskell, 58 bytes

f n=do i<-[1..n];[' '|odd$div 0x888889249557c$2^i]++"beep"

Try it online!

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

Perl 5, 54 bytes

$_=" beep"x$_;s/ /0x444444924aabe>>$x&$x++<51?$&:""/ge

Try it online!

Ungolfed a bit:

$_=" beep"x$_;           # create string of space+beep the input number of times
s/ /                     # remove spaces unless it's space number
0x444444924aabe          # 1 2 3 4 5 7 9 11 13 15 18 21 24 27 30
                         # 34 38 42 46 or 50 (counting from zero)
                         # 0x444444924aabe in binary have 1's on
                         # those positions
>>$x                     # keep space if 1-bit and space number <= 50
&$x++<51?$&:""/ge        # remove space if not
\$\endgroup\$

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.