Skip to content

Commit

Permalink
day 140: just a repeat of day 149
Browse files Browse the repository at this point in the history
  • Loading branch information
vaskoz committed Dec 24, 2019
1 parent 2648a51 commit e483786
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
13 changes: 13 additions & 0 deletions day400/problem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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
}
30 changes: 30 additions & 0 deletions day400/problem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package day149

import "testing"

// nolint
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 e483786

Please sign in to comment.