『情熱プログラマー』面白いですね。
今回はその中で出てきた「99 Bottles of Beer」の歌詞を出力するプログラムを書きます。
99 Bottles of Beer
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でした。
解答集
この解答が面白かった。
以上です。