How to resolve the algorithm Old lady swallowed a fly step by step in the Lua programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Old lady swallowed a fly step by step in the Lua programming language

Table of Contents

Problem Statement

Present a program which emits the lyrics to the song   I Knew an Old Lady Who Swallowed a Fly,   taking advantage of the repetitive structure of the song's lyrics. This song has multiple versions with slightly different lyrics, so all these programs might not emit identical output.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Old lady swallowed a fly step by step in the Lua programming language

Source code in the lua programming language

animals = {"fly", "spider", "bird", "cat","dog", "goat", "cow", "horse"}
phrases = {
    "",
    "That wriggled and jiggled and tickled inside her",
    "How absurd to swallow a bird",
    "Fancy that to swallow a cat",
    "What a hog, to swallow a dog",
    "She just opened her throat and swallowed a goat",
    "I don't know how she swallowed a cow",
    "  ...She's dead of course"
}

for i=0,7 do
    io.write(string.format("There was an old lady who swallowed a %s\n", animals[i+1]))
    if i>0 then io.write(phrases[i+1]) end
    if i==7 then break end
    if i>0 then
        io.write("\n")
        for j=i,1,-1 do
            io.write(string.format("She swallowed the %s to catch the %s", animals[j+1], animals[j]))
            -- if j<4 then p='.' else p=',' end
            -- io.write(string.format("%s\n", p))
            io.write("\n")
            if j==2 then
                io.write(string.format("%s!\n", phrases[2]))
            end
        end
    end
    io.write("I don't know why she swallowed a fly - Perhaps she'll die!\n\n")
end


  

You may also check:How to resolve the algorithm Distributed programming step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the Slope programming language
You may also check:How to resolve the algorithm Find duplicate files step by step in the Tcl programming language
You may also check:How to resolve the algorithm Base64 decode data step by step in the C# programming language
You may also check:How to resolve the algorithm Stair-climbing puzzle step by step in the Scala programming language