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 2a3f6ed

Browse files
author
Sandy
authored
Merge pull request #188 from openset/develop
Add: Pascal's Triangle II
2 parents ae58c0f + 5af694b commit 2a3f6ed

File tree

2 files changed

+42
-0
lines changed

2 files changed

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

0 commit comments

Comments
(0)

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