-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |