Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

99 Shortened Bottles of Beer

+5
−0

Disclaimer

This challenge also exists in CGCC, but if you want to compete here (too), then hop in!

Challenge

Recreate "99 Bottles of Beer on the Wall", using the least bytes possible.

Lyrics:

99 bottles of beer on the wall, 99 bottles of beer.
Take one down and pass it around, 98 bottles of beer on the wall.

98 bottles of beer on the wall, 98 bottles of beer.
Take one down and pass it around, 97 bottles of beer on the wall.

97 bottles of beer on the wall, 97 bottles of beer.
Take one down and pass it around, 96 bottles of beer on the wall.

96 bottles of beer on the wall, 96 bottles of beer.
Take one down and pass it around, 95 bottles of beer on the wall.

95 bottles of beer on the wall, 95 bottles of beer.
Take one down and pass it around, 94 bottles of beer on the wall.

....

3 bottles of beer on the wall, 3 bottles of beer.
Take one down and pass it around, 2 bottles of beer on the wall.

2 bottles of beer on the wall, 2 bottles of beer.
Take one down and pass it around, 1 bottle of beer on the wall.

1 bottle of beer on the wall, 1 bottle of beer.
Go to the store and buy some more, 99 bottles of beer on the wall.

Rules

  • 0-byte answers aren't allowed. Although 1-byte answers are allowed while still abiding to not using loopholes, they won't be very interesting (unless you DO make it interesting).
  • Use any form of output in the program. Yes, the whole thing must be outputted up until you reach $n=1$.
  • Built-in functions and/or code for this specific challenge is pretty much allowed, only if you explain (even a little) how the code works (or not if you can't at all).
  • Shortest program of each language used is the winner.
History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

2 comment threads

Are leading or trailing newlines permitted? (2 comments)
Are snippets allowed? (2 comments)

13 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+2
−0

C (gcc), 244 bytes

main(){for(char*w=" bottles of beer on the wall",i=100;--i;)printf("%d%.*s%s, %d%.*s%.8s.\n%s, %d%.*s%s.\n\n",i,i-1?8:7,w,w+8,i,i-1?8:7,w,w+8,i^1?"Take one down and pass it around":"Go to the store and buy some more",i^1?i-1:99,i^2?8:7,w,w+8);}

Try it online!

Full program, gcc extensions.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

C (gcc), 232 bytes

f(n,w,p){printf("%i bottle%s of beer%s%s",n,"s"+(n<2)," on the wall"+w,p);}main(i){for(i=99;i;){f(i,0,", ");f(i,12,".\n");printf(i>1?"Take one down and pass it around, ":"Go to the store and buy some more, ");f(--i?:99,0,".\n\n");}}

Try it online!

Main golfing techniques:

  • The repetitive part has been put into a function

  • conditional strings are sometimes implemented using pointer arithmetic, using the fact that C strings are zero terminated character arrays

  • The code uses the implicit int rule

  • I'm passing a pointer through an int argument which then in turn is interpreted as a pointer by printf (not portable and definitely not clean; allows me to use the implicit int rule for that argument)

  • The gcc extension condition?:value (give condition unless that is zero, in which case give value) is used to save one character

  • The printf function is declared implicitly (which isn't guaranteed to work by any C standard, but works just fine in practice by any compiler that allows implicit declarations), saving the 18 characters that would otherwise be needed for #include<stdio.h> (including the mandatory line break following that directive)

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

Nice one (3 comments)
+2
−0

FALSE, 198 bytes

[" on the wall"]c:[$." bottle"$1=~["s"]?" of beer"]b:99[$1>][b;!c;!", "b;!".
Take one down, pass it around, "1-b;!c;!".

"]#1b;!" on the wall, "1b;!".
Go to the store and buy some more, "99b;!c;!"."

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

Haskell, 221 215 bytes

f=a 99
(#)=(++)
a n=d n#", "#c".\n"n#b(n-1)
b 0="Go to the store and buy some more, "#d 99#"."
b n="Take one down and pass it around, "#d n#".\n\n"#a n
c m n=show n#" bottle"#['s'|n>1]#" of beer"#m
d=c" on the wall"

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

Java (JDK), 311 301 296 bytes

interface A{static void main(String[]x){String a=" bottle",b=" of beer",c=" on the wall. ",e;for(int i=99;i>0;i--){e=i>1?"s":"";System.out.println(i+a+e+b+c+(i)+a+e+b+(i>1?".\nTake one down and pass it around. ":".\nGo to the store and buy some more. ")+(i>1?i-1:99)+a+(i!=2?"s":"")+b+c+"\n");}}}

Try it online!

This shortened if-else condition thingy is actually pretty good.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

Python 3, 202 bytes

a,i='bottle%s of beer on the wall',99
while i:i-=1;x=a%'s'[:i];print(i+1,x+',',i+1,x[:-12]+'.\n'+'GToa kteo  otnhee  dsotwonr ea nadn dp absusy  isto maer omuonrde,,'[i>0::2],i or 99,a%'s'[:i!=1]+'.\n')

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+2
−0

Scala, 242 bytes

_=>99 to(1,-1)map{b=>s"${w(b)} on the wall, ${w(b)}. \n${if(b>1)"Take one down and pass it around"else
"Go to the store and buy some more"}, ${w((b+97)%99+1)} on the wall."}mkString "\n\n"
def w(b:Int)=s"$b bottle${if(b>1)"s"else ""} of beer"

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

JavaScript (Node.js), 228 224 bytes

-4 bytes thanks to Hakerh400

(b=n=>`${n} bottle${n-1?'s':''} of beer`,w=' on the wall')=>[...Array(99)].map((_,i)=>`${b(i=99-i)}${w}, ${b(i)}.
`+(--i?`Take one down and pass it around, `:(i=99,`Go to the store and buy some more, `))+b(i)+w+'.').join`

`

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

1 comment thread

General comments (2 comments)
+1
−0

Japt, 117 116 115 bytes

Needs more golfing or, perhaps, a completely different approach. Borrows a couple of tricks trick from ETHProductions' original version.

Includes 2 trailing newlines.

´LÇ=L+` Þ­¤{´LÎçs}  Þ8`".

{=Z+`  e Ø!`}, {Z}.
{LÎg`Go ‘ e ÐJe „d ¿y Ñ  ڐ
Take e ܵ „d p†s Š ÂИ`·}, "iUìé~H

Test it

´LÇ=L+`...{´LÎçs}...`"...{=Z+`...`}...{Z}...{LÎg`...`·}..."iUìé~H
´L                                                                     :Prefix decrement L (initially 100)
  Ç                                                                    :Map each Z in the range [0,L)
   =                                                                   :  Reassign to Z
    L+                                                                 :    L concatenated with
      `                                                                :    Compressed string
       ...                                                             :      " bottle"
          {                                                            :      Interpolate
           ´LÎ                                                         :        Prefix decrement L and get its sign
              çs                                                       :        Repeat "s" that many times
                }                                                      :      End interpolate
                 ...                                                   :      " of beer"
                    `                                                  :    End compressed string
                     "...                                              :  Literal string (the return value)
                         {                                             :    Interpolate
                          =                                            :      Assign to variable U
                           X+`...`                                     :      X & the compressed string " on the wall"
                                  }                                    :    End interpolate
                                   ...                                 :    Literals
                                      {X}                              :    Interpolate D
                                         ...                           :    Literals
                                            {                          :    Interpolate
                                             LÎg                       :      Get the sign of L and index (0-based) into
                                                `...`                  :        Compressed string "Go to the store and buy some more\nTake one down and pass it around"
                                                     ·                 :        Split on newlines
                                                      }                :    End interpolate
                                                       ..."            :  End of string
                                                           iU          :  Prepend U
                                                             Ã         :End map
                                                              ¬        :Join
                                                               é       :Rotate right by
                                                                ~H     :  The bitwise NOT of 32 (i.e., -33)
History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

shortC, 223 bytes

AOC*w=" bottles of beer on the wall",i=100;--i;)R"%d%.*s%s, %d%.*s%.8s.\n%s, %d%.*s%s.\n\n",i,i-1?8:7,w,w+8,i,i-1?8:7,w,w+8,i^1?"Take one down and pass it around":"Go to the store and buy some more",i^1?i-1:99,i^2?8:7,w,w+8

Try it online!

From @Lundin's C (gcc) answer.

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

Sclipting, (UTF-16) 238 bytes

눰 감下❶貶下標⓶긆깯덇끬뉗꼠닶눠눦녥댠❷감侔是검摧終併終❷긆뭮긇끨뉒걷눖롬 껂밀⓹껠뙔눖띥긆뭮뉒걤닷덮긆굮뉂거눗꽳긆땴긆굲닷녮뉂렠⓺❺껠똊終棄棄껠뙇닲건닲건늆넠댷끯댦넠눖멤긆깵뎒걳닶륥긆륯댦넬긃딹긆깯덇끬뉗꼠닶눠눦녥댢걯닢건늆넠덶구닂밎

Explanation

눰 감下
For n from 99 down to 1
    ❶貶下
    For k from n to n - 1
        標⓶긆깯덇끬뉗꼠닶눠눦녥댠
        Push " bottles of beer"
        ❷감侔是검摧終
        If k == 1, delete the 8th character from the end (the s)
        併
        Combine into a string -> "k bottle(s) of beer"
    終
    End loop
    ❷
    Copy "n bottles of beer" from the stack
    긆뭮긇끨뉒걷눖롬 껂밀
    " on the wall" ", "
    ⓹
    Move "n bottles of beer" from the stack
    껠뙔눖띥긆뭮뉒걤닷덮긆굮뉂거눗꽳긆땴긆굲닷녮뉂렠
    ".\nTake one down and pass it around, "
    ⓺❺
    Move "<n-1> bottles of beer". Copy " on the wall".
    껠똊
    ".\n\n"
終
End loop
棄棄
Discard the top four items (which are the zero bottles text)
껠뙇닲건닲건늆넠댷끯댦넠눖멤긆깵뎒걳닶륥긆륯댦넬긃딹긆깯덇끬뉗꼠닶눠눦녥댢걯닢건늆넠덶구닂밎
".\nGo to the store and buy some more, 99 bottles of beer on the wall."
History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+1
−0

JavaScript (V8), 185 bytes

for(c=99;c;)print((f=(s=` on the wall`)=>`${c||99} bottle${~-c?`s`:``} of beer`+s)()+`, ${f``}.
${--c?`Take one down and pass it around`:`Go to the store and buy some more`}, ${f()}.
`)

Try it online!

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

+0
−0

Seriously, 1 byte

N

Try it online!

Yep. lmao

History
Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »