Add arrays package
This commit is contained in:
		| @@ -7,3 +7,17 @@ func Sum(numbers []int) int { | ||||
| 	} | ||||
| 	return sum | ||||
| } | ||||
|  | ||||
| func SumAllTails(numbersToSum ...[]int) []int { | ||||
| 	sums := make([]int, 0, len(numbersToSum)) | ||||
|  | ||||
| 	for _, numbers := range numbersToSum { | ||||
| 		if len(numbers) == 0 { | ||||
| 			sums = append(sums, 0) | ||||
| 		} else { | ||||
| 			tail := numbers[1:] | ||||
| 			sums = append(sums, Sum(tail)) | ||||
| 		} | ||||
| 	} | ||||
| 	return sums | ||||
| } | ||||
|   | ||||
| @@ -1,6 +1,9 @@ | ||||
| package main | ||||
|  | ||||
| import "testing" | ||||
| import ( | ||||
| 	"reflect" | ||||
| 	"testing" | ||||
| ) | ||||
|  | ||||
| func TestSum(t *testing.T) { | ||||
|  | ||||
| @@ -27,3 +30,33 @@ func TestSum(t *testing.T) { | ||||
| 	}) | ||||
|  | ||||
| } | ||||
|  | ||||
| //func TestSumAll(t *testing.T) { | ||||
| //got := SumAll([]int{1, 2}, []int{0, 9}) | ||||
| //want := []int{3, 9} | ||||
|  | ||||
| //if !reflect.DeepEqual(got, want) { | ||||
| //t.Errorf("got %v want %v", got, want) | ||||
| //} | ||||
| //} | ||||
|  | ||||
| func TestSumAllTails(t *testing.T) { | ||||
| 	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("make the sums of tails of", func(t *testing.T) { | ||||
| 		got := SumAllTails([]int{1, 2}, []int{0, 9}) | ||||
| 		want := []int{2, 9} | ||||
| 		checkSums(t, got, want) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("safely sum empty slices", func(t *testing.T) { | ||||
| 		got := SumAllTails([]int{}, []int{3, 4, 5}) | ||||
| 		want := []int{0, 9} | ||||
| 		checkSums(t, got, want) | ||||
| 	}) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user