diff options
| author | mo khan <mo@mokhan.ca> | 2021-11-07 16:02:42 -0700 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2021-11-07 16:02:42 -0700 |
| commit | 51c05013fff2c817ffb4a8649eed8ee2836e95b1 (patch) | |
| tree | f2c62e9fb496a61ca205e914c9067af3289d6961 /exercises/4.1-2 | |
| parent | 1af2c5f253f426327f61c819ea576b6c70adc940 (diff) | |
answer a few more questions
Diffstat (limited to 'exercises/4.1-2')
| -rw-r--r-- | exercises/4.1-2/max_subarray_test.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/exercises/4.1-2/max_subarray_test.go b/exercises/4.1-2/max_subarray_test.go new file mode 100644 index 0000000..920a3fb --- /dev/null +++ b/exercises/4.1-2/max_subarray_test.go @@ -0,0 +1,43 @@ +package main + +import ( + "fmt" + "testing" +) + +func MaxSubArray(a []int, low int, high int) (int, int, int) { + left := low + right := high + sum := -1 + + for i := low; i < high; i++ { + tempSum := 0 + for j := i; j < high; j++ { + tempSum = tempSum + a[j] + if tempSum > sum { + sum = tempSum + left = i + right = j + } + } + } + return left, right, sum +} + +func TestMergeSort(t *testing.T) { + t.Run("", func(t *testing.T) { + items := []int{100, 113, 110, 85, 105, 102, 86, 63, 81, 101, 94, 106, 101, 79, 94, 90, 97} + l, r, total := MaxSubArray(items, 0, len(items)) + + fmt.Println(l, r, total) + if l != 0 { + t.Errorf("Expected: %v, Got: %v", 0, l) + } + if r != 16 { + t.Errorf("Expected: %v, Got: %v", 16, r) + } + if total != 1607 { + t.Errorf("Expected: %v, Got: %v", 1607, total) + } + }) +} |
