Sha256: 26024884f0c9905b4fcfec375756ab325815bc05fc33e7afcec4664643353f86

Contents?: true

Size: 1.83 KB

Versions: 115

Compression:

Stored size: 1.83 KB

Contents

package beer

import (
	"bytes"
	"fmt"
)

const testVersion = 1

// Song returns the full lyrics for 99 bottles of beer
func Song() (result string) {
	result, _ = Verses(99, 0)
	return
}

// Verses returns an exerpt of the lyrics of 99 bottles of beer
// between verse start and stop. The verse numbers count backwards, so the
// first verse sung will be verse 99, and the last will be verse 0
func Verses(start, stop int) (string, error) {
	switch {
	case 0 > start || start > 99:
		return "", fmt.Errorf("start value[%d] is not a valid verse", start)
	case 0 > stop || stop > 99:
		return "", fmt.Errorf("stop value[%d] is not a valid verse", stop)
	case start < stop:
		return "", fmt.Errorf("start value[%d] is less than stop value[%d]", start, stop)
	}

	var buff bytes.Buffer
	for i := start; i >= stop; i-- {
		v, _ := Verse(i)
		buff.WriteString(v)
		buff.WriteString("\n")
	}
	return buff.String(), nil
}

// Verse returns a single verse of the lyrics of 99 bottles of beer. The verse
// numbers count backwards, so the first verse sung will be verse 99, and the
// last will be verse 0
func Verse(n int) (string, error) {
	result := ""
	switch {
	case 0 > n || n > 99:
		return "", fmt.Errorf("%d is not a valid verse", n)
	case n == 0:
		result = "No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n"
	case n == 1:
		result = "1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n"
	case n == 2:
		result = "2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n"
	default:
		result = fmt.Sprintf("%d bottles of beer on the wall, %d bottles of beer.\nTake one down and pass it around, %d bottles of beer on the wall.\n", n, n, n-1)
	}
	return result, nil
}

Version data entries

115 entries across 115 versions & 1 rubygems

Version Path
trackler-2.0.8.14 tracks/go/exercises/beer-song/example.go
trackler-2.0.8.13 tracks/go/exercises/beer-song/example.go
trackler-2.0.8.12 tracks/go/exercises/beer-song/example.go
trackler-2.0.8.11 tracks/go/exercises/beer-song/example.go
trackler-2.0.8.10 tracks/go/exercises/beer-song/example.go
trackler-2.0.8.9 tracks/go/exercises/beer-song/example.go
trackler-2.0.8.8 tracks/go/exercises/beer-song/example.go
trackler-2.0.8.7 tracks/go/exercises/beer-song/example.go
trackler-2.0.8.6 tracks/go/exercises/beer-song/example.go
trackler-2.0.8.5 tracks/go/exercises/beer-song/example.go
trackler-2.0.8.4 tracks/go/exercises/beer-song/example.go
trackler-2.0.8.3 tracks/go/exercises/beer-song/example.go
trackler-2.0.8.2 tracks/go/exercises/beer-song/example.go
trackler-2.0.8.1 tracks/go/exercises/beer-song/example.go
trackler-2.0.7.0 tracks/go/exercises/beer-song/example.go
trackler-2.0.6.44 tracks/go/exercises/beer-song/example.go
trackler-2.0.6.43 tracks/go/exercises/beer-song/example.go
trackler-2.0.6.42 tracks/go/exercises/beer-song/example.go
trackler-2.0.6.41 tracks/go/exercises/beer-song/example.go
trackler-2.0.6.40 tracks/go/exercises/beer-song/example.go