x = "012345" s = x[0:3] # s contains "012" s = x[:3] # same thing (using default) s = x[4:] # s contains "45" s = x[1:4] # s contains "123" s = x[-5:-2] # same thing using negative indices s = x[-2:99] # s contains "45" - negative index from end of # string allowed, as is index outside string (99) s = x[::2] # s contains "024" s = x[1::2] # s contains "135" s = x[1:4:2] # s contains "13" s = x[1:5:2] # s contains "13" s = x[1:6:2] # s contains "135" s = x[-1::-2] # s contains "531" s = x[::-2] # same thing (using default) s = x[::-1] # s contains "543210" (i.e. string reverse) s = x[::0] # "ValueError: slice step cannot be zero" #### n=99 z=lambda:`n or 99`+" bottle"+"s of beer on the wall"[n==1:] while n:y=z();n-=1;print"%s, %s.\n"*2%(y,y[:-12],n and"Take one down and pass it around"or"Go to the store and buy some more",z()) #### n and"Take one down and pass it around"or"Go to the store and buy some more" #### "Take"if n else"Go to" (n>0)*"Take"or"Go to" ["Go to","Take"][n>0] ("Go to","Take")[n>0] n and"Take"or"Go to" "GToa kteo"[n>0::2] # "Slice and Dice" wins this golf! #### 99 bottles of beer on the wall, 99 bottles of beer.\nTake one down and pass it around, 98 bottles of beer on the wall.\n\n #### i=298 while~-i:print i/3or 99,'bottle'+'s of beer on the wall.\n\n'[23)+i%3*68::2],;i-=1 #### '..\n\nGToa kteo otnhee dsotwonr ea nadn dp absusy isto maer omuonrde,,'[(i>3)+i%3*68::2] #### i=298 while~-i:print i/3or 99,'bottle'+'s of beer on the wall'[23::2],',','.\n\n')[i%3],;i-=1 #### i=298 while~-i:print i/3or 99,'bottle'+'s of beer on the wall.\n\n'[2## i=297 while i:print-~i/3or 99,'bottle'+'s of beer on the wall.\n\n'[1## print~$_/3|0||99," bottle",substr("s of beer on the wall. X",6/$_%2,-4**($_%3)),(", ",$_<-4?". Take one down and pass it around, ":". Go to the store and buy some more, ")[$_%3-3]for-299..-3 #### print-$_/3|0||99," bottle",("s"," of beer"," on the wall",". ")[5/$_%2..-$_%3+1],($_<-3?". Take one down and pass it around, ":". Go to the store and buy some more, ",", ")[-$_%3]for-298..-2 #### 298.downto(2){|i|$><3?". Take one down and pass it around, ":". Go to the store and buy some more, ",", "][i%3]} #### 298.downto(2){|i|$><## eval"$><<%{#{a=b='#{n} bottl#{"es"[0,n]} of beer',' on the wall'}, #{b}. \#{0<(n-=1)?'Take one down and pass it around':(n=99;'Go to the store and buy some more')}, #{a}. };"*n=99 #### eval %{puts"#{a='#{n} bottle#{n>1?:s:p} of beer on the wall'}, #{a[0,30]}. \#{(n-=1)<1?(n=99;'Go to the store and buy some more'):'Take one down and pass it around'}, #{a}. " }*n=99 #### $_="Take one down and pass it around, #{~/s/;c=4-$.-=1," bottle#$& of beer"," on the wall"}. #{c}, %s%s. #$_"%c until/, 99\D+/;puts$'+"Go to the store and buy some more"+$& #### "Go to the store and buy some more"+(-99..-1).map{|n|", #{b=-n," bottles"[0,6-n]+" of beer"," on the wall"}. #{b}, %s%s. "%b}*"Take one down and pass it around"=~/ /;puts$'+$` #### # Based on emiltin. eval"$><<%{#{a=b='#{n} bottl#{"es"[0,n]} of beer',' on the wall'}, #{b}. \#{0<(n-=1)?'Take one down and pass it around':'Go to the store and buy some more'%n=99}, #{a}. };"*n=99 #### # Based on J-_-L. eval"$><<%{#{a=b='#{n} bottle#{n>1?:s:p} of beer',' on the wall'}, #{b}. \#{(n-=1)<1?'Go to the store and buy some more'%n=99:'Take one down and pass it around'}, #{a}. } "*n=99 #### /s/until@c=(@b=(++$n,bottle.$&,of,beer),on,the,wall),s/^/Take one down and pass it around, @c. @c, @b. /,/, 99\D+/;print$'."Go to the store and buy some more$&" #### $_="Take one down and pass it around, #{~/s/;c=4-$.-=1," bottle#$& of beer"," on the wall"}. #{c}, %s%s. #$_"%c until/, 99\D+/;puts$'+"Go to the store and buy some more"+$& #### i=298 while~-i:print i/3or 99,'bottle'+'s of beer on the wall.\n\n'[5/i%i:i%3*28^9]+'.X\n.G\noT atkoe tohnee sdtoowrne aanndd pbausys siotm ea rmoourned,,'[i%3*69^3%i::2],;i-=1