九十九瓶啤酒
* * * *
拉格朗日计划
* * * *
九十九瓶啤酒

打印民谣《九十九瓶啤酒》的歌词。

歌词如下:

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瓶啤酒。。。)

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.


本题难度:



解答

歌词共100段,段落之间以空行分割。

前99段每段第一句的数字不同,最后一段第一句两个分句的句首分别是大小写的no more表示0。

前98段最后一句的数字不同,第99段最后一句的no more指代0,最后一段的最后一句整句与前99段都不同。

匿名方法中用x控制数字的显示,y控制大小写。所有涉及分支判断的,都用列表+下标选择这一短码技巧。

最终代码只有两行。

代码长度:249字节 vs. 全站第一:206字节。

a=lambda x,y:f"{['nN'[y]+'o more',str(x%100)][x!=0]} bottle{'s'*(x!=1)} of beer on the wall"
for i in range(99,-1,-1):print(f"{a(i,1)}, {a(i,0)[:-12]}.\n{['Go to the store and buy some more','Take one down and pass it around'][i>0]}, {a(i-1,0)}.\n")