Repository: vaskoz/dailycodingproblem-go (go)

Issue:
Day 295
Good morning! Here's your coding interview problem for today.

This problem was asked by Stitch Fix.

Pascal's triangle is a triangular array of integers constructed with the following formula:

The first row consists of the number 1.
For each subsequent row, each element is the sum of the numbers directly above it, on either side.
For example, here are the first few rows:
```
    1
   1 1
  1 2 1
 1 3 3 1
1 4 6 4 1
```
Given an input `k`, return the kth row of Pascal's triangle.

Bonus: Can you do this using only O(k) space?

Pull request description:
Day295
Resolves #601.

Patch:
diff --git a/README.md b/README.md
index e9aac2b..6c9d998 100644
--- a/README.md
+++ b/README.md
@@ -301,3 +301,4 @@ problems from
 * [Day 288](https://github.com/vaskoz/dailycodingproblem-go/issues/590)
 * [Day 290](https://github.com/vaskoz/dailycodingproblem-go/issues/594)
 * [Day 291](https://github.com/vaskoz/dailycodingproblem-go/issues/596)
+* [Day 295](https://github.com/vaskoz/dailycodingproblem-go/issues/601)
diff --git a/day295/problem.go b/day295/problem.go
new file mode 100644
index 0000000..79d9fea
--- /dev/null
+++ b/day295/problem.go
@@ -0,0 +1,23 @@
+package day295
+
+// PascalTriangleRow returns a particular row in Pascal's triangle.
+// Runs in O(row) time and space.
+func PascalTriangleRow(row int) []int {
+	result := make([]int, row+1)
+	for entry := range result {
+		result[entry] = combinations(row, entry)
+	}
+	return result
+}
+
+func combinations(n, k int) int {
+	return factorial(n) / (factorial(k) * factorial(n-k))
+}
+
+func factorial(n int) int {
+	result := 1
+	for i := 2; i <= n; i++ {
+		result *= i
+	}
+	return result
+}


Test patch:
diff --git a/day295/problem_test.go b/day295/problem_test.go
new file mode 100644
index 0000000..4190bdb
--- /dev/null
+++ b/day295/problem_test.go
@@ -0,0 +1,38 @@
+package day295
+
+import (
+	"reflect"
+	"testing"
+)
+
+// nolint
+var testcases = []struct {
+	row      int
+	expected []int
+}{
+	{0, []int{1}},
+	{1, []int{1, 1}},
+	{2, []int{1, 2, 1}},
+	{3, []int{1, 3, 3, 1}},
+	{4, []int{1, 4, 6, 4, 1}},
+	{5, []int{1, 5, 10, 10, 5, 1}},
+	{6, []int{1, 6, 15, 20, 15, 6, 1}},
+	{7, []int{1, 7, 21, 35, 35, 21, 7, 1}},
+}
+
+func TestPascalTriangleRow(t *testing.T) {
+	t.Parallel()
+	for _, tc := range testcases {
+		if result := PascalTriangleRow(tc.row); !reflect.DeepEqual(result, tc.expected) {
+			t.Errorf("Expected %v, got %v", tc.expected, result)
+		}
+	}
+}
+
+func BenchmarkPascalTriangleRow(b *testing.B) {
+	for i := 0; i < b.N; i++ {
+		for _, tc := range testcases {
+			PascalTriangleRow(tc.row)
+		}
+	}
+}