How to resolve the algorithm 99 bottles of beer step by step in the Go programming language
How to resolve the algorithm 99 bottles of beer step by step in the Go 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 Go programming language
First Exapmle:
-
Defines a function called
bottles
that takes an integeri
and based on the value ofi
, returns a string describing the number of bottles of beer. -
In the
main
function:- Uses a for loop to iterate from 99 to 1, decrementing by 1 each iteration.
- For each value of
i
, it prints the following lines:- The number of bottles of beer on the wall, using the
bottles
function to format the string. - The number of bottles of beer
- "Take one down, pass it around"
- The number of bottles of beer on the wall after decrementing by 1.
- The number of bottles of beer on the wall, using the
Second Example:
-
Uses more advanced string manipulation and randomness to create a more stylized version of the song.
-
Defines a function called
numberName
that converts a number to its name (e.g., 1 to "one", 12 to "twelve"). -
Defines a function called
pluralizeFirst
that makes the first word of a string plural by adding an "s" if the second argument is not 1. -
Defines a function called
slur
that simulates drunkenness by shuffling the interior letters of a string and condensing any consecutive spaces into a single space. -
In the
main
function:- Seeds a random number generator with the current time.
- Uses a for loop to iterate from 99 to 1, decrementing by 1 each iteration.
- For each value of
i
, it prints the following lines:- The number of bottles of beer on the wall, using
numberName
andpluralizeFirst
to format the string, andslur
to simulate drunkenness. - The number of bottles of beer, using the same formatting functions.
- "Take one down, pass it around", using
slur
to simulate drunkenness. - The number of bottles of beer on the wall after decrementing by 1, using the same formatting functions.
- The number of bottles of beer on the wall, using
Source code in the go programming language
package main
import "fmt"
func main() {
bottles := func(i int) string {
switch i {
case 0:
return "No more bottles"
case 1:
return "1 bottle"
default:
return fmt.Sprintf("%d bottles", i)
}
}
for i := 99; i > 0; i-- {
fmt.Printf("%s of beer on the wall\n", bottles(i))
fmt.Printf("%s of beer\n", bottles(i))
fmt.Printf("Take one down, pass it around\n")
fmt.Printf("%s of beer on the wall\n", bottles(i-1))
}
}
package main
import (
"fmt"
"math/rand"
"strings"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
for i := 99; i > 0; i-- {
fmt.Printf("%s %s %s\n",
slur(numberName(i), i),
pluralizeFirst(slur("bottle of", i), i),
slur("beer on the wall", i))
fmt.Printf("%s %s %s\n",
slur(numberName(i), i),
pluralizeFirst(slur("bottle of", i), i),
slur("beer", i))
fmt.Printf("%s %s %s\n",
slur("take one", i),
slur("down", i),
slur("pass it around", i))
fmt.Printf("%s %s %s\n",
slur(numberName(i-1), i),
pluralizeFirst(slur("bottle of", i), i-1),
slur("beer on the wall", i))
}
}
// adapted from Number names task
func numberName(n int) string {
switch {
case n < 0:
case n < 20:
return small[n]
case n < 100:
t := tens[n/10]
s := n % 10
if s > 0 {
t += " " + small[s]
}
return t
}
return ""
}
var small = []string{"no", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen",
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"}
var tens = []string{"ones", "ten", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety"}
// pluralize first word of s by adding an s, but only if n is not 1.
func pluralizeFirst(s string, n int) string {
if n == 1 {
return s
}
w := strings.Fields(s)
w[0] += "s"
return strings.Join(w, " ")
}
// p is string to slur, d is drunkenness, from 0 to 99
func slur(p string, d int) string {
// shuffle only interior letters
a := []byte(p[1 : len(p)-1])
// adapted from Knuth shuffle task.
// shuffle letters with probability d/100.
for i := len(a) - 1; i >= 1; i-- {
if rand.Intn(100) >= d {
j := rand.Intn(i + 1)
a[i], a[j] = a[j], a[i]
}
}
// condense spaces
w := strings.Fields(p[:1] + string(a) + p[len(p)-1:])
return strings.Join(w, " ")
}
You may also check:How to resolve the algorithm Loops/While step by step in the 6502 Assembly programming language
You may also check:How to resolve the algorithm Problem of Apollonius step by step in the F# programming language
You may also check:How to resolve the algorithm Calculating the value of e step by step in the Java programming language
You may also check:How to resolve the algorithm Spinning rod animation/Text step by step in the Perl programming language
You may also check:How to resolve the algorithm Random numbers step by step in the NetRexx programming language