Sha256: 9b587b6f0c196e9ef709da598fd7785a260b53da7b31772302d79440a3124e90

Contents?: true

Size: 1.27 KB

Versions: 274

Compression:

Stored size: 1.27 KB

Contents

package kindergarten

import (
	"errors"
	"sort"
	"strings"
)

const testVersion = 1

type Garden map[string][]string

func (g *Garden) Plants(child string) ([]string, bool) {
	p, ok := (*g)[child]
	return p, ok
}

func NewGarden(diagram string, children []string) (*Garden, error) {
	rows := strings.Split(diagram, "\n")
	if len(rows) != 3 || len(rows[0]) != 0 {
		return nil, errors.New("diagram must have two rows")
	}
	if len(rows[1]) != len(rows[2]) {
		return nil, errors.New("diagram rows must be same length")
	}
	if len(rows[1]) != 2*len(children) {
		return nil, errors.New("each diagram row must have two cups per child")
	}
	g := Garden{}
	alpha := append([]string{}, children...)
	sort.Strings(alpha)
	for _, n := range alpha {
		g[n] = make([]string, 0, 4)
	}
	if len(g) != len(alpha) {
		return nil, errors.New("no two children can have the same name")
	}
	for _, row := range rows[1:] {
		for nx, n := range alpha {
			for cx := range rows[1:] { // a little hack
				var p string
				switch row[2*nx+cx] {
				case 'G':
					p = "grass"
				case 'C':
					p = "clover"
				case 'R':
					p = "radishes"
				case 'V':
					p = "violets"
				default:
					return nil, errors.New("plant codes must be one of G, C, R, or V")
				}
				g[n] = append(g[n], p)
			}
		}
	}
	return &g, nil
}

Version data entries

274 entries across 274 versions & 1 rubygems

Version Path
trackler-2.2.1.16 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.15 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.14 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.13 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.12 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.11 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.10 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.9 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.8 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.7 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.6 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.5 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.4 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.3 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.2 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.1 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.0 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.0.6 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.0.5 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.0.4 tracks/go/exercises/kindergarten-garden/example.go