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.56 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.55 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.54 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.53 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.52 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.51 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.50 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.49 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.48 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.47 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.46 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.45 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.44 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.43 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.42 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.41 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.40 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.39 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.38 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.2.1.37 tracks/go/exercises/kindergarten-garden/example.go