exercism/go/pascals-triangle/pascal.go

27 lines
608 B
Go
Raw Normal View History

2016-08-13 23:20:14 +00:00
package pascal
// Triangle returns pascals triangle up to depth
func Triangle(depth int) [][]int {
var ret [][]int
for currDepth := 0; currDepth < depth; currDepth++ {
var newRow, prevRow []int
// Each row is currDepth+1 long
rowLength := currDepth + 1
// Rows start with a 1
newRow = append(newRow, 1)
if currDepth > 0 {
prevRow = ret[currDepth-1]
}
for i := 1; i < rowLength; i++ {
if i == rowLength-1 {
// Rows end with a 1 also
newRow = append(newRow, 1)
break
}
newRow = append(newRow, prevRow[i-1]+prevRow[i])
}
ret = append(ret, newRow)
}
return ret
}