Log

いろいろ

99 Bottles of Beer

『情熱プログラマー』面白いですね。

情熱プログラマー ソフトウェア開発者の幸せな生き方

情熱プログラマー ソフトウェア開発者の幸せな生き方

  • 作者:Chad Fowler
  • 出版社/メーカー: オーム社
  • 発売日: 2010/02/26
  • メディア: 単行本(ソフトカバー)
 

今回はその中で出てきた「99 Bottles of Beer」の歌詞を出力するプログラムを書きます。

99 Bottles of Beer

www.99-bottles-of-beer.net

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.

(中略)
1 bottle of beer on the wall, 1 bottle of beer.
Take one down and pass it around, no more bottles of beer on the wall.

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

Beer! Beer! Beer!

Pythonで書いてみる

for i in range(99, 0, -1):
    print('{0} of beer on the wall, {0} of beer.'.format(f'{i} bottles' if i>1 else '1 bottle'))
    print('Take one down and pass it around, {0} of beer on the wall.\n'.format(f'{i-1} bottles' if i>2 else '1 bottle' if i==2 else 'no more bottles'))

print('No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.')

なるはやで短く書いたつもり。ファイルサイズは416Bでした。

解答集

この解答が面白かった。

www.99-bottles-of-beer.net

以上です。