Skip to content

Instantly share code, notes, and snippets.

@Bobochka
Created September 23, 2017 12:16
Show Gist options
  • Select an option

  • Save Bobochka/a859cca3fc7134c538dab3054a6869f2 to your computer and use it in GitHub Desktop.

Select an option

Save Bobochka/a859cca3fc7134c538dab3054a6869f2 to your computer and use it in GitHub Desktop.
Pascal triangle
package main
import (
"fmt"
"reflect"
)
func main() {
resGot := pascalTriangle(6)
resWant := [][]int{
{1},
{1, 1},
{1, 2, 1},
{1, 3, 3, 1},
{1, 4, 6, 4, 1},
{1, 5, 10, 10, 5, 1},
}
if !reflect.DeepEqual(resGot, resWant) {
fmt.Println("Wrong result:", resGot)
}
}
func pascalTriangle(n int) [][]int {
res := [][]int{}
for i := 0; i < n; i++ {
line := make([]int, i+1)
line[0] = 1
line[i] = 1
for j := 1; j < i; j++ {
line[j] = res[i-1][j-1] + res[i-1][j]
}
res = append(res, line)
}
return res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment