Sha256: d842a702420a2ce8259c9060bbbd1a8da3cff571e8df56743ba2e459b9bba242

Contents?: true

Size: 957 Bytes

Versions: 122

Compression:

Stored size: 957 Bytes

Contents

package diamond

import (
	"errors"
	"strings"
)

const startIndex = 'A'

// Gen builds a diamond
func Gen(char byte) (string, error) {
	if char > 'Z' || char < 'A' {
		return "", errors.New(string(char) + " isn't supported, only between (A, Z)")
	}
	return gen(char), nil
}

func gen(char byte) string {
	var output []string
	currentIndex := int(char - startIndex)
	for i := 0; i <= currentIndex; i++ {
		output = append(output, getLine(currentIndex, i))
	}
	for i := currentIndex - 1; i > -1; i-- {
		output = append(output, getLine(currentIndex, i))
	}
	return strings.Join(output, "\n") + "\n"
}

func getLine(currentStart, current int) string {
	diff := currentStart - current
	return spaces(diff) + alphabets(current) + spaces(diff)
}

func alphabets(current int) string {
	if current == 0 {
		return "A"
	}
	c := current + startIndex
	return string(c) + spaces(current*2-1) + string(c)
}

func spaces(n int) string {
	return strings.Repeat(" ", n)
}

Version data entries

122 entries across 122 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/go/exercises/diamond/example.go
trackler-2.2.1.179 tracks/go/exercises/diamond/example.go
trackler-2.2.1.178 tracks/go/exercises/diamond/example.go
trackler-2.2.1.177 tracks/go/exercises/diamond/example.go
trackler-2.2.1.176 tracks/go/exercises/diamond/example.go
trackler-2.2.1.175 tracks/go/exercises/diamond/example.go
trackler-2.2.1.174 tracks/go/exercises/diamond/example.go
trackler-2.2.1.173 tracks/go/exercises/diamond/example.go
trackler-2.2.1.172 tracks/go/exercises/diamond/example.go
trackler-2.2.1.171 tracks/go/exercises/diamond/example.go
trackler-2.2.1.170 tracks/go/exercises/diamond/example.go
trackler-2.2.1.169 tracks/go/exercises/diamond/example.go
trackler-2.2.1.167 tracks/go/exercises/diamond/example.go
trackler-2.2.1.166 tracks/go/exercises/diamond/example.go
trackler-2.2.1.165 tracks/go/exercises/diamond/example.go
trackler-2.2.1.164 tracks/go/exercises/diamond/example.go
trackler-2.2.1.163 tracks/go/exercises/diamond/example.go
trackler-2.2.1.162 tracks/go/exercises/diamond/example.go
trackler-2.2.1.161 tracks/go/exercises/diamond/example.go
trackler-2.2.1.160 tracks/go/exercises/diamond/example.go