How to resolve the algorithm 99 bottles of beer step by step in the Dart programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm 99 bottles of beer step by step in the Dart 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 Dart programming language
Source code in the dart programming language
main() {
BeerSong beerSong = BeerSong();
//pass a 'starting point' and 'end point' as parameters respectively
String printTheLyrics = beerSong.recite(99, 1).join('\n');
print(printTheLyrics);
}
class Song {
String bottleOnTheWall(int index) {
String bottleOnTheWallText =
'$index bottles of beer on the wall, $index bottles of beer,';
return bottleOnTheWallText;
}
String bottleTakenDown(int index) {
String englishGrammar = (index >= 2) ? 'bottle' : 'bottles';
String bottleTakenDownText =
'Take one down and pass it around, ${index - 1} $englishGrammar of beer on the wall.';
return bottleTakenDownText;
}
}
class BeerSong extends Song {
String bottleOnTheWall(int index) {
String originalText = super.bottleOnTheWall(index);
if (index < 2) {
String bottleOnTheWallText =
'$index bottle of beer on the wall, $index bottle of beer,';
return bottleOnTheWallText;
}
return originalText;
}
String bottleTakenDown(int index) {
if (index < 2) {
String bottleTakenDownText =
'Take it down and pass it around, no more bottles of beer on the wall.';
return bottleTakenDownText;
}
String originalText = super.bottleTakenDown(index);
return originalText;
}
List<String> recite(int actualBottleOnTheWall, int remainingBottleOnTheWall) {
List<String> theLyrics = [];
for (int index = actualBottleOnTheWall;
index >= remainingBottleOnTheWall;
index--) {
String onTheWall = bottleOnTheWall(index);
String takenDown = bottleTakenDown(index);
theLyrics.add(onTheWall);
theLyrics.add(takenDown);
theLyrics.add('');
}
return theLyrics;
}
}
}
You may also check:How to resolve the algorithm Strip comments from a string step by step in the Forth programming language
You may also check:How to resolve the algorithm Day of the week step by step in the Tcl programming language
You may also check:How to resolve the algorithm Farey sequence step by step in the Raku programming language
You may also check:How to resolve the algorithm Catamorphism step by step in the RPL programming language
You may also check:How to resolve the algorithm Sort a list of object identifiers step by step in the Elixir programming language