summaryrefslogtreecommitdiff
path: root/go/diamond/diamond.go
blob: 37e74fd21478346819f8725c4bba6a55dae52cf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package diamond

import (
	"bytes"
	"errors"
)

const testVersion = 1

func Gen(b byte) (string, error) {
	if b < 'A' || b > 'Z' {
		return "", errors.New("out of range")
	}
	x := int(b - 'A')
	rows := make([][]byte, 2*x+1)
	for i, j := x, 0; i >= 0; i, j = i-1, j+1 {
		line := bytes.Repeat([]byte{' '}, 2*x+2)
		line[2*x+1] = '\n'
		char := 'A' + byte(j)
		line[i], line[2*x-i] = char, char
		rows[j], rows[2*x-j] = line, line
	}
	return string(bytes.Join(rows, nil)), nil
}