From eeef9622dfd2db6ed7480a512e870f733ce747ed Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Thu, 7 Mar 2024 11:16:57 -0500 Subject: [PATCH] Finished up arrays and slice lesson --- learn_go_with_tests/arrays/go.mod | 2 +- learn_go_with_tests/arrays/sum.go | 36 +++++++++++++++++++++++++- learn_go_with_tests/arrays/sum_test.go | 26 +++++++++++++++++++ 3 files changed, 62 insertions(+), 2 deletions(-) diff --git a/learn_go_with_tests/arrays/go.mod b/learn_go_with_tests/arrays/go.mod index 88bf4e7..fcc2e9c 100644 --- a/learn_go_with_tests/arrays/go.mod +++ b/learn_go_with_tests/arrays/go.mod @@ -1,3 +1,3 @@ -module myarray +module arrays go 1.21.0 diff --git a/learn_go_with_tests/arrays/sum.go b/learn_go_with_tests/arrays/sum.go index 2bd2ec5..94ccccf 100644 --- a/learn_go_with_tests/arrays/sum.go +++ b/learn_go_with_tests/arrays/sum.go @@ -27,5 +27,39 @@ func Sum(a []int) int { // SumAll func SumAll(numbersToSum ...[]int) []int { - return nil + // lengthOfNumbers := len(numbersToSum) + + //There's a new way to create a slice. make allows you to create a slice with a starting capacity of the len of the numbersToSum we need to work through. + // sums := make([]int, lengthOfNumbers) + + // for i, numbers := range numbersToSum { + // sums[i] = Sum(numbers) + // } + + // Using the append method you do not have to worry about capacity and index errors + // append will create a new slice with greater capacity and the new value added to it. + // The is a different between len() and capacity() capacity is the allocated size, and there + // is an algorithm used by append to determine when it needs to change. + var sums []int + + for _, numbers := range numbersToSum { + sums = append(sums, Sum(numbers)) + } + + return sums +} + +func SumAllTails(numbersToSum ...[]int) []int { + var sums []int + + for _, numbers := range numbersToSum { + if len(numbers) == 0 { + sums = append(sums, 0) + } else { + tails := numbers[1:] + sums = append(sums, Sum(tails)) + } + } + + return sums } diff --git a/learn_go_with_tests/arrays/sum_test.go b/learn_go_with_tests/arrays/sum_test.go index 3ead916..1c275e0 100644 --- a/learn_go_with_tests/arrays/sum_test.go +++ b/learn_go_with_tests/arrays/sum_test.go @@ -81,3 +81,29 @@ func TestSumAll(t *testing.T) { t.Errorf("Using slices.Equal got %v expected %v given", got, expected) } } + +// The tail of a collection is all items in the collection except the first one (the "head"). +func TestSumAllTails(t *testing.T) { + + // Another example of helper function + checkSums := func(t testing.TB, got, want []int) { + t.Helper() + if !reflect.DeepEqual(got, want) { + t.Errorf("got %v want %v", got, want) + } + } + + t.Run("test slices of same lenght", func(t *testing.T) { + got := SumAllTails([]int{1, 2}, []int{0, 9}) + want := []int{2, 9} + + checkSums(t, got, want) + }) + + t.Run("test an empty slice and slice of different len", func(t *testing.T) { + got := SumAllTails([]int{}, []int{0, 4, 5}) + want := []int{0, 9} + + checkSums(t, got, want) + }) +}