-
Notifications
You must be signed in to change notification settings - Fork 0
/
diamond.go
59 lines (49 loc) · 1.38 KB
/
diamond.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package diamond
import (
. "strings"
)
//ToDiamond ...
// return a slice of strings that when printed form a diamond!
//i
//0 A
//1 --B B outerPadding = 2
//2 C C
//3 D D
//2 C---C innerPadding = 3
//1 B B
//0 A
//
func ToDiamond(letter string) []string {
index := Index("ABCDEFGHIJKLMNOPQRSTUVWXYZ", letter)
// Get the first line:
output := []string{firstOrLastline(index)}
if index == 0 {
return output
}
// Get the first slope of the diamond (and the middle point):
for i := 1; i <= index; i++ {
innerPadding := (i * 2) - 1
outerPadding := index - i
thisLetter := letterForIndex(i)
output = append(output, anotherLine(thisLetter, outerPadding, innerPadding))
}
// Get the second slope of the diamond:
for i := index - 1; i > 0; i-- {
innerPadding := (i * 2) - 1
outerPadding := index - i
thisLetter := letterForIndex(i)
output = append(output, anotherLine(thisLetter, outerPadding, innerPadding))
}
// Get the last line:
output = append(output, firstOrLastline(index))
return output
}
func letterForIndex(index int) string {
return string("ABCDEFGHIJKLMNOPQRSTUVWXYZ"[index])
}
func firstOrLastline(index int) string {
return Join([]string{Repeat(" ", index), "A"}, "")
}
func anotherLine(letter string, outerPadding, innerPadding int) string {
return Join([]string{Repeat(" ", outerPadding), letter, Repeat(" ", innerPadding), letter}, "")
}