Skip to content

Commit

Permalink
Merge aa017f8 into fe7553e
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Jan 22, 2019
2 parents fe7553e + aa017f8 commit af284b1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,4 @@ problems from
* [Day 146](https://github.com/vaskoz/dailycodingproblem-go/issues/303)
* [Day 147](https://github.com/vaskoz/dailycodingproblem-go/issues/304)
* [Day 148](https://github.com/vaskoz/dailycodingproblem-go/issues/309)
* [Day 149](https://github.com/vaskoz/dailycodingproblem-go/issues/311)
12 changes: 12 additions & 0 deletions day149/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package day149

// SumSublist returns the sum of the sublist from start (inclusive)
// up to end (exclusive).
// Runs in O(end-start).
func SumSublist(L []int, start, end int) int {
var sum int
for i := start; i < end; i++ {
sum += L[i]
}
return sum
}
28 changes: 28 additions & 0 deletions day149/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package day149

import "testing"

var testcases = []struct {
L []int
start, end int
expected int
}{
{[]int{1, 2, 3, 4, 5}, 1, 3, 5},
}

func TestSumSublist(t *testing.T) {
t.Parallel()
for _, tc := range testcases {
if result := SumSublist(tc.L, tc.start, tc.end); result != tc.expected {
t.Errorf("Expected %v got %v", tc.expected, result)
}
}
}

func BenchmarkSumSublist(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, tc := range testcases {
SumSublist(tc.L, tc.start, tc.end)
}
}
}

0 comments on commit af284b1

Please sign in to comment.