From 1e2700a2f221a7e3ba5a67115a9191e646a3002b Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 16 Dec 2025 20:22:52 -0500 Subject: [PATCH] fix: december --- pkg/product_of_array_except_self/main.go | 25 +++++++++++++++++++ pkg/product_of_array_except_self/main_test.go | 16 ++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 pkg/product_of_array_except_self/main.go create mode 100644 pkg/product_of_array_except_self/main_test.go diff --git a/pkg/product_of_array_except_self/main.go b/pkg/product_of_array_except_self/main.go new file mode 100644 index 0000000..bf0c71c --- /dev/null +++ b/pkg/product_of_array_except_self/main.go @@ -0,0 +1,25 @@ +package product_of_array_except_self + +func ProductExceptSelf(nums []int) []int { + result := []int{} + + for range nums { + result = append(result, 1) + } + + lower_sum := 1 + + for i := range len(nums) { + result[i] *= lower_sum + lower_sum *= nums[i] + } + + upper_sum := 1 + + for i := range len(nums) { + result[len(nums)-1-i] *= upper_sum + upper_sum *= nums[len(nums)-1-i] + } + + return result +} diff --git a/pkg/product_of_array_except_self/main_test.go b/pkg/product_of_array_except_self/main_test.go new file mode 100644 index 0000000..5d3783e --- /dev/null +++ b/pkg/product_of_array_except_self/main_test.go @@ -0,0 +1,16 @@ +package product_of_array_except_self_test + +import ( + "testing" + + "git.maximhutz.com/practice/pkg/product_of_array_except_self" + "github.com/stretchr/testify/assert" +) + +func Test1(t *testing.T) { + assert.Equal(t, []int{24, 12, 8, 6}, product_of_array_except_self.ProductExceptSelf([]int{1, 2, 3, 4})) +} + +func Test2(t *testing.T) { + assert.Equal(t, []int{0, 0, 9, 0, 0}, product_of_array_except_self.ProductExceptSelf([]int{-1, 1, 0, -3, 3})) +}