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.1.0.39 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.1.0.38 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.1.0.37 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.1.0.36 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.1.0.34 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.1.0.33 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.1.0.32 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.1.0.31 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.1.0.30 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.1.0.29 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.1.0.28 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.1.0.27 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.1.0.26 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.1.0.25 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.1.0.24 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.1.0.23 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.1.0.22 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.1.0.21 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.1.0.20 tracks/go/exercises/kindergarten-garden/example.go
trackler-2.1.0.19 tracks/go/exercises/kindergarten-garden/example.go