exercism/go/diamond/diamond.go

37 lines
693 B
Go
Raw Normal View History

2016-11-30 03:28:40 +00:00
package diamond
import (
"errors"
"strings"
)
const testVersion = 1
func Gen(c byte) (string, error) {
if c > 'Z' || c < 'A' {
return "", errors.New("Out of Range")
}
d := rune(strings.ToUpper(string(c))[0])
var ret string
for i := 'A'; i <= d; i++ {
ret += PrintRow(i, d) + "\n"
}
for i := d - 1; i >= 'A'; i-- {
ret += PrintRow(i, d) + "\n"
}
return ret, nil
}
func PrintRow(rowChar rune, genChar rune) string {
var ret string
ret += strings.Repeat(" ", int(genChar-rowChar))
ret += string(rowChar)
if rowChar > 'A' {
ret += strings.Repeat(" ", (2*int(rowChar-'A') - 1))
ret += string(rowChar)
}
ret += strings.Repeat(" ", int(genChar-rowChar))
return ret
}