Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 43ce826

Browse files
author
Openset
committed
Add: Pascal's Triangle
1 parent 01684f0 commit 43ce826

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
11
package pascals_triangle
2+
3+
func generate(numRows int) [][]int {
4+
ans := make([][]int, numRows)
5+
for i := 0; i < numRows; i++ {
6+
row := make([]int, i+1)
7+
row[0], row[i] = 1, 1
8+
for j := 1; j < i; j++ {
9+
row[j] = ans[i-1][j-1] + ans[i-1][j]
10+
}
11+
ans[i] = row
12+
}
13+
return ans
14+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,32 @@
11
package pascals_triangle
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
type caseType struct {
9+
input int
10+
expected [][]int
11+
}
12+
13+
func TestGenerate(t *testing.T) {
14+
tests := [...]caseType{
15+
{
16+
input: 5,
17+
expected: [][]int{
18+
{1},
19+
{1, 1},
20+
{1, 2, 1},
21+
{1, 3, 3, 1},
22+
{1, 4, 6, 4, 1},
23+
},
24+
},
25+
}
26+
for _, tc := range tests {
27+
output := generate(tc.input)
28+
if !reflect.DeepEqual(output, tc.expected) {
29+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
30+
}
31+
}
32+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /