99 Bottles of Beer (#144)
I recently¹ found a five year old StackExchange thread talking about a programming challenge to recreate the marvelously imaginative lyrics of “99 Bottles of Beer”. The song goes as follows (whole lyrics can be viewed below).
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.
[…]
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.
Of course, the real challenge is not to build a program that generates the lyrics but rather to do it with the least amount of characters². To achieve this goal, I tried out various strategies, all written in Python.
The first strategy is to join a string generated by a for loop (263 characters).
The second, less efficient strategy is to use a function and recursion (274 characters).
The third is a bit more character-efficient, using lambda functions and recursion (270 characters).
The fourth, final and most space-efficient³ strategy is to use a while loop (250 characters).
[1] | [2020-07-21] One may note that I at the time of this post was not a codegolf.StackExchange member. |
[2] | [2020-07-27] N. b.: A source file’s trailing newline does not count towards the golf score. |
[3] | [2020-07-21] That I came up with at that time. |