How to resolve the algorithm 99 bottles of beer step by step in the Nim programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm 99 bottles of beer step by step in the Nim 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 Nim programming language
Source code in the nim programming language
proc getBottleNumber(n: int): string =
case n
of 0:
result = "No more bottles"
of 1:
result = "1 bottle"
else:
result = $n & " bottles"
result &= " of beer"
for bn in countdown(99, 1):
let cur = getBottleNumber(bn)
echo(cur, " on the wall, ", cur, ".")
echo("Take one down and pass it around, ", getBottleNumber(bn-1), " on the wall.\n")
echo "No more bottles of beer on the wall, no more bottles of beer."
echo "Go to the store and buy some more, 99 bottles of beer on the wall."
from strutils import format
for i in countdown(99, 1):
case i
of 3..99:
echo format("""$1 bottles of beer on the wall
$1 bottles of beer
Take one down, pass it around
$2 bottles of beer on the wall""", i, i-1)
of 2:
echo format("""$1 bottles of beer on the wall
$1 bottles of beer
Take one down, pass it around
$2 bottle of beer on the wall""", i, i-1)
of 1:
echo format("""$1 bottle of beer on the wall
$1 bottle of beer
Take one down, pass it around
No more bottles of beer on the wall""", i)
else:
discard
from strutils import format
proc pluralize(a: int): string =
if a > 1 or a == 0: "s"
else: ""
for i in countdown(99, 1):
echo format("""$1 bottle$3 of beer on the wall
$1 bottle$3 of beer
Take one down, pass it around
$2 bottle$4 of beer on the wall""", i, i-1, pluralize(i), pluralize(i-1))
from strutils import format
var verse = """$1 bottle$3 of beer on the wall
$1 bottle$3 of beer
Take one down, pass it around
$2 bottle$4 of beer on the wall"""
proc pluralize(a: int): string =
if a > 1 or a == 0: "s"
else: ""
for i in countdown(99, 1):
echo format(verse, i, i-1, pluralize(i), pluralize(i-1))
You may also check:How to resolve the algorithm Carmichael 3 strong pseudoprimes step by step in the Factor programming language
You may also check:How to resolve the algorithm Binary digits step by step in the ZX Spectrum Basic programming language
You may also check:How to resolve the algorithm Polynomial regression step by step in the Lua programming language
You may also check:How to resolve the algorithm Read a file line by line step by step in the SNOBOL4 programming language
You may also check:How to resolve the algorithm Hunt the Wumpus step by step in the SparForte programming language