How to write a test case for product sum question

huangapple go评论138阅读模式
英文:

How to write a test case for product sum question

问题

我正在尝试为以下代码编写一个测试用例,该代码用于查找嵌套数组的乘积和总和:

package main

import "fmt"

type SpecialArray []interface{}

func ProductSum(array SpecialArray) int {
        return helper(array, 1)
}

func helper(array SpecialArray, multiplier int) int {
        sum := 0
        for _, el := range array {
                if cast, ok := el.(SpecialArray); ok {
                        sum += helper(cast, multiplier + 1)
                } else if cast, ok := el.(int); ok {
                        sum += cast
                }
        }
        return sum * multiplier

}

给定一个包含整数和数组的嵌套数组,上述代码应返回所有元素的总和。嵌套数组中的元素将被求和,然后乘以它们的深度,即,

[1, [2, 3], 2] = 1 + 2(2+3) + 2 = 13

我对 SpecialArray []interface{} 类型的理解有些困惑。我最初的尝试是在我的 main 函数中简单地定义一个嵌套切片,如下所示:

func main() {
        SpecialArray := [][]int{1, {2, 3}, 2}
        result := ProductSum(SpecialArray)
        fmt.Println(result)
}

但是这并不起作用。我猜想我需要使用值初始化我的 SpecialArray 接口,并将其传递给我的 ProductSum 函数。

英文:

I am trying to write a test case in my main function for the following code that is used to find the product and sum of a nested array:

package main

import "fmt"

type SpecialArray []interface{}

func ProductSum(array SpecialArray) int {
        return helper(array, 1)
}

func helper(array SpecialArray, multiplier int) int {
        sum := 0
        for _, el := range array {
                if cast, ok := el.(SpecialArray); ok {
                        sum += helper(cast, multiplier + 1)
                } else if cast, ok := el.(int); ok {
                        sum += cast
                }
        }
        return sum * multiplier

}

Given a nested array with a mix of ints and arrays, the above code should return the sum of all elements. Elements within a nested array will be summed then multiplied by their depth, i.e.,

[1, [2, 3], 2] = 1 + 2(2+3) + 2 = 13

I am having trouble understanding the SpecialArray []interface{} type. My first attempt was to simply define a nested slice in my main function as follows:

func main() {
        SpecialArray := [][]int{1, {2, 3}, 2}
        result := ProductSum(SpecialArray)
        fmt.Println(result)
}

But this is not working. I'm assuming I need to initalize my SpecialArray interface with values and pass that to my ProductSum function.

答案1

得分: 2

你非常接近了,但是数组字面量的语法还不太正确:

func main() {
	special := SpecialArray{1, SpecialArray{2, 3}, 2}
	result := ProductSum(special)
	fmt.Println(result)
}

另外,你的 helper 函数是正确的,但是你可以考虑使用类型切换(type switch):

func helper(array SpecialArray, multiplier int) int {
	sum := 0
	for _, v := range array {
		switch v := v.(type) {
		case int:
			sum += v
		case SpecialArray:
			sum += helper(v, multiplier+1)
		default:
			log.Fatalf("未知类型:%T", v)
		}
	}
	return sum * multiplier
}

顺便说一下,SpecialArray 类型是一个切片(slice),而不是数组。

英文:

You are very close but did not quite get the array literal syntax right:

func main() {
	special := SpecialArray{1, SpecialArray{2, 3}, 2}
	result := ProductSum(special)
	fmt.Println(result)
}

Also your helper function is fine as is but you might consider using a type switch:

func helper(array SpecialArray, multiplier int) int {
	sum := 0
	for _, v := range array {
		switch v := v.(type) {
		case int:
			sum += v
		case SpecialArray:
			sum += helper(v, multiplier+1)
		default:
			log.Fatalf("Unknown type: %T", v)
		}
	}
	return sum * multiplier
}

BTW the SpecialArray type is a slice not an array.

huangapple
  • 本文由 发表于 2022年7月7日 12:08:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/72892179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定