How to resolve the algorithm 99 bottles of beer step by step in the Lua programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm 99 bottles of beer step by step in the Lua programming language

Table of Contents

Problem Statement

Display the complete lyrics for the song:     99 Bottles of Beer on the Wall.

The lyrics follow this form: ... and so on, until reaching   0     (zero). Grammatical support for   1 bottle of beer   is optional. As with any puzzle, try to do it in as creative/concise/comical a way as possible (simple, obvious solutions allowed, too).

Let's start with the solution:

Step by Step solution about How to resolve the algorithm 99 bottles of beer step by step in the Lua programming language

Source code in the lua programming language

local bottles = 99

local function plural (bottles) if bottles == 1 then return '' end return 's' end
while bottles > 0 do
    print (bottles..' bottle'..plural(bottles)..' of beer on the wall')
    print (bottles..' bottle'..plural(bottles)..' of beer')
    print ('Take one down, pass it around')
    bottles = bottles - 1
    print (bottles..' bottle'..plural(bottles)..' of beer on the wall')
    print ()
end

verse = [[%i bottle%s of beer on the wall
%i bottle%s of beer
Take one down, pass it around
%i bottle%s of beer on the wall
]]
function suffix(i) return i ~= 1 and 's' or '' end

for i = 99, 1, -1 do
    print(verse:format(i, suffix(i), i, suffix(i), i-1, suffix(i-1)))
end

function bottles(i)
  local s = i == 1 and "1 bottle of beer" or
            i == 0 and "no more bottles of beer" or 
            tostring(i) .. " bottles of beer"
  return s, s
end

for i = 99, 1, -1 do
  print( string.format("%s on the wall,\n%s,\ntake one down, pass it around,", bottles(i)),
         string.format("\n%s on the wall.\n", bottles(i-1)) )
end

  

You may also check:How to resolve the algorithm Here document step by step in the Applesoft BASIC programming language
You may also check:How to resolve the algorithm Padovan n-step number sequences step by step in the Java programming language
You may also check:How to resolve the algorithm Multiplicative order step by step in the C# programming language
You may also check:How to resolve the algorithm Sierpinski triangle step by step in the OCaml programming language
You may also check:How to resolve the algorithm Create a file step by step in the Component Pascal programming language