Sha256: ca8f65b3f4c343c8d8e1e9fcfd0e06a892c4b980243992ad6e40e700413cab2b

Contents?: true

Size: 980 Bytes

Versions: 262

Compression:

Stored size: 980 Bytes

Contents

package diamond

import (
	"errors"
	"strings"
)

const testVersion = 1

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

262 entries across 262 versions & 1 rubygems

Version Path
trackler-2.2.1.56 tracks/go/exercises/diamond/example.go
trackler-2.2.1.55 tracks/go/exercises/diamond/example.go
trackler-2.2.1.54 tracks/go/exercises/diamond/example.go
trackler-2.2.1.53 tracks/go/exercises/diamond/example.go
trackler-2.2.1.52 tracks/go/exercises/diamond/example.go
trackler-2.2.1.51 tracks/go/exercises/diamond/example.go
trackler-2.2.1.50 tracks/go/exercises/diamond/example.go
trackler-2.2.1.49 tracks/go/exercises/diamond/example.go
trackler-2.2.1.48 tracks/go/exercises/diamond/example.go
trackler-2.2.1.47 tracks/go/exercises/diamond/example.go
trackler-2.2.1.46 tracks/go/exercises/diamond/example.go
trackler-2.2.1.45 tracks/go/exercises/diamond/example.go
trackler-2.2.1.44 tracks/go/exercises/diamond/example.go
trackler-2.2.1.43 tracks/go/exercises/diamond/example.go
trackler-2.2.1.42 tracks/go/exercises/diamond/example.go
trackler-2.2.1.41 tracks/go/exercises/diamond/example.go
trackler-2.2.1.40 tracks/go/exercises/diamond/example.go
trackler-2.2.1.39 tracks/go/exercises/diamond/example.go
trackler-2.2.1.38 tracks/go/exercises/diamond/example.go
trackler-2.2.1.37 tracks/go/exercises/diamond/example.go