How to resolve the algorithm Old lady swallowed a fly step by step in the Kotlin programming language
Published on 22 June 2024 08:30 PM
How to resolve the algorithm Old lady swallowed a fly step by step in the Kotlin 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 Kotlin programming language
Kotlin Program to Solve the Rhyme “There was an Old Lady who Swallowed a Fly”
In this program, the rhyme "There was an Old Lady who Swallowed a Fly" is implemented using Kotlin language. This code takes the animals and phrases from the rhyme and presents them in the correct order. Let's break down the code step by step:
-
Data Initialization:
- Two lists named
animals
andphrases
are defined. These lists contain the animals and phrases used in the rhyme, respectively.
- Two lists named
-
sing()
Function:- The
sing()
function forms the core of the program and is responsible for printing the rhyme. It iterates through each animal in theanimals
list and prints the corresponding phrase from thephrases
list. - For each animal, it checks if it is the first animal (index
i
is 0). If not, it prints the exclamation mark after the phrase. - If it's the last animal (index
i
is 7), it returns early to terminate the loop. - Before moving to the next animal, it prints the lines that describe the old lady swallowing the previous animals to catch the current one. This loop runs from
i
down to 1 (inclusive). - It then prints the final line of the verse.
- The
-
main()
Function:- The
main()
function is the entry point of the program. It simply calls thesing()
function to execute the rhyme.
- The
Output:
When you run the program, it will print the following output:
There was an old lady who swallowed a fly;
I don't know why she swallowed a fly - Perhaps she'll die!
There was an old lady who swallowed a spider;
That wriggled and jiggled and tickled inside her!
She swallowed the spider to catch the fly;
I don't know why she swallowed a fly - Perhaps she'll die!
There was an old lady who swallowed a bird;
How absurd to swallow a bird!
She swallowed the bird to catch the spider,
That wriggled and jiggled and tickled inside her!
She swallowed the spider to catch the fly;
I don't know why she swallowed a fly - Perhaps she'll die!
There was an old lady who swallowed a cat;
Fancy that to swallow a cat!
She swallowed the cat to catch the bird,
How absurd to swallow a bird!
She swallowed the bird to catch the spider,
That wriggled and jiggled and tickled inside her!
She swallowed the spider to catch the fly;
I don't know why she swallowed a fly - Perhaps she'll die!
There was an old lady who swallowed a dog;
What a hog, to swallow a dog!
She swallowed the dog to catch the cat,
Fancy that to swallow a cat!
She swallowed the cat to catch the bird,
How absurd to swallow a bird!
She swallowed the bird to catch the spider,
That wriggled and jiggled and tickled inside her!
She swallowed the spider to catch the fly;
I don't know why she swallowed a fly - Perhaps she'll die!
There was an old lady who swallowed a goat;
She just opened her throat and swallowed a goat!
She swallowed the goat to catch the dog,
What a hog, to swallow a dog!
She swallowed the dog to catch the cat,
Fancy that to swallow a cat!
She swallowed the cat to catch the bird,
How absurd to swallow a bird!
She swallowed the bird to catch the spider,
That wriggled and jiggled and tickled inside her!
She swallowed the spider to catch the fly;
I don't know why she swallowed a fly - Perhaps she'll die!
There was an old lady who swallowed a cow;
I don't know how she swallowed a cow!
She swallowed the cow to catch the goat,
She just opened her throat and swallowed a goat!
She swallowed the goat to catch the dog,
What a hog, to swallow a dog!
She swallowed the dog to catch the cat,
Fancy that to swallow a cat!
She swallowed the cat to catch the bird,
How absurd to swallow a bird!
She swallowed the bird to catch the spider,
That wriggled and jiggled and tickled inside her!
She swallowed the spider to catch the fly;
I don't know why she swallowed a fly - Perhaps she'll die!
...She's dead of course!
Source code in the kotlin programming language
// version 1.1.3
val animals = listOf("fly", "spider", "bird", "cat","dog", "goat", "cow", "horse")
val phrases = listOf(
"",
"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",
"\n ...She's dead of course"
)
fun sing() {
for (i in 0..7) {
println("There was an old lady who swallowed a ${animals[i]};")
if (i > 0) println("${phrases[i]}!")
if (i == 7) return
println()
if (i > 0) {
for (j in i downTo 1) {
print(" She swallowed the ${animals[j]} to catch the ${animals[j - 1]}")
println(if (j < 3) ";" else ",")
if (j == 2) println(" ${phrases[1]}!")
}
}
println(" I don't know why she swallowed a fly - Perhaps she'll die!\n")
}
}
fun main(args: Array<String>) {
sing()
}
You may also check:How to resolve the algorithm Program termination step by step in the Erlang programming language
You may also check:How to resolve the algorithm Sudoku step by step in the RPN (HP-15c) programming language
You may also check:How to resolve the algorithm Closures/Value capture step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Horizontal sundial calculations step by step in the Sather programming language
You may also check:How to resolve the algorithm Here document step by step in the C# programming language