Created
September 23, 2017 12:16
-
-
Save Bobochka/a859cca3fc7134c538dab3054a6869f2 to your computer and use it in GitHub Desktop.
Pascal triangle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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