Skip to content

Commit eb39cb7

Browse files
i-redbyteraklaptudirmgithub-action
authored
Feat/pascal (TheAlgorithms#433)
Co-authored-by: Rak Laptudirm <[email protected]> Co-authored-by: github-action <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent 37cb38e commit eb39cb7

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,16 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
565565

566566
1. [`IsPalindrome`](./strings/palindrome/ispalindrome.go#L26): No description provided.
567567

568+
---
569+
</details><details>
570+
<summary> <strong> pascal </strong> </summary>
571+
572+
---
573+
574+
##### Functions:
575+
576+
1. [`GenerateTriangle`](./math/pascal/pascaltriangle.go#L24): GenerateTriangle This function generates a Pascal's triangle of n lines
577+
568578
---
569579
</details><details>
570580
<summary> <strong> password </strong> </summary>

math/pascal/pascaltriangle.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// pascaltriangle.go
2+
// description: Pascal's triangle
3+
// details:
4+
// Pascal's triangle is a triangular array of the binomial coefficients that arises in probability theory, combinatorics, and algebra. - [Pascal's triangle](https://en.wikipedia.org/wiki/Pascal%27s_triangle)
5+
// example:
6+
//1
7+
//1 1
8+
//1 2 1
9+
//1 3 3 1
10+
//1 4 6 4 1
11+
//1 5 10 10 5 1
12+
//1 6 15 20 15 6 1
13+
//1 7 21 35 35 21 7 1
14+
//1 8 28 56 70 56 28 8 1
15+
//1 9 36 84 126 126 84 36 9 1
16+
//1 10 45 120 210 252 210 120 45 10 1
17+
//...
18+
// author(s) [red_byte](https://github.com/i-redbyte)
19+
// see pascaltriangle_test.go
20+
21+
package pascal
22+
23+
// GenerateTriangle This function generates a Pascal's triangle of n lines
24+
func GenerateTriangle(n int) [][]int {
25+
var triangle = make([][]int, n)
26+
for i := 0; i < n; i++ {
27+
triangle[i] = make([]int, i+1)
28+
triangle[i][0], triangle[i][i] = 1, 1
29+
for j := 1; j < i; j++ {
30+
triangle[i][j] = triangle[i-1][j] + triangle[i-1][j-1]
31+
}
32+
}
33+
return triangle
34+
}

math/pascal/pascaltriangle_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// pascaltriangle_test.go
2+
// description: Test for Pascal's triangle
3+
// author(s) [red_byte](https://github.com/i-redbyte)
4+
// see pascaltriangle.go
5+
6+
package pascal
7+
8+
import (
9+
"reflect"
10+
"testing"
11+
)
12+
13+
func TestGenerateTriangle(t *testing.T) {
14+
tests := []struct {
15+
name string
16+
n int
17+
want [][]int
18+
}{
19+
{name: "Pascal's three-line triangle", n: 3, want: [][]int{{1}, {1, 1}, {1, 2, 1}}},
20+
{name: "Pascal's 0-line triangle", n: 0, want: [][]int{}},
21+
{name: "Pascal's one-line triangle", n: 1, want: [][]int{{1}}},
22+
{name: "Pascal's 7-line triangle", n: 7, want: [][]int{{1}, {1, 1}, {1, 2, 1}, {1, 3, 3, 1}, {1, 4, 6, 4, 1}, {1, 5, 10, 10, 5, 1}, {1, 6, 15, 20, 15, 6, 1}}},
23+
}
24+
for _, test := range tests {
25+
t.Run(test.name, func(t *testing.T) {
26+
if got := GenerateTriangle(test.n); !reflect.DeepEqual(got, test.want) {
27+
t.Errorf("GenerateTriangle() = %v, want %v", got, test.want)
28+
}
29+
})
30+
}
31+
}
32+
33+
func BenchmarkGenerateTriangle(b *testing.B) {
34+
for i := 0; i < b.N; i++ {
35+
GenerateTriangle(10)
36+
}
37+
}

0 commit comments

Comments
 (0)