// Package diamond outputs input in diamond shape.
package diamond
import (
"fmt"
"strings"
)
func Gen(in byte) (string, error) {
if in > 'Z' || in < 'A' {
return "", fmt.Errorf("out of range")
}
var ret strings.Builder
pivot := int(in - 'A')
for i := 0; i <= pivot; i++ {
ret.WriteString(writeLine(pivot, i))
}
for i := pivot - 1; i >= 0; i-- {
// start at pivot-1 as we do not want to dupe middle line
ret.WriteString(writeLine(pivot, i))
}
// fmt.Println(ret.String())
return ret.String(), nil
}
func writeLine(pivot, i int) string {
const mark = " " // set to "." for testing
var ret strings.Builder
ret.WriteString(strings.Repeat(mark, pivot-i))
ret.WriteByte(byte(i) + 'A')
if i != 0 {
ret.WriteString(strings.Repeat(mark, i*2-1))
ret.WriteByte(byte(i) + 'A')
}
ret.WriteString(strings.Repeat(mark, pivot-i))
ret.WriteByte('\n')
return ret.String()
}