How to write a test case for product sum question

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

How to write a test case for product sum question

问题

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

  1. package main
  2. import "fmt"
  3. type SpecialArray []interface{}
  4. func ProductSum(array SpecialArray) int {
  5. return helper(array, 1)
  6. }
  7. func helper(array SpecialArray, multiplier int) int {
  8. sum := 0
  9. for _, el := range array {
  10. if cast, ok := el.(SpecialArray); ok {
  11. sum += helper(cast, multiplier + 1)
  12. } else if cast, ok := el.(int); ok {
  13. sum += cast
  14. }
  15. }
  16. return sum * multiplier
  17. }

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

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

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

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

但是这并不起作用。我猜想我需要使用值初始化我的 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:

  1. package main
  2. import "fmt"
  3. type SpecialArray []interface{}
  4. func ProductSum(array SpecialArray) int {
  5. return helper(array, 1)
  6. }
  7. func helper(array SpecialArray, multiplier int) int {
  8. sum := 0
  9. for _, el := range array {
  10. if cast, ok := el.(SpecialArray); ok {
  11. sum += helper(cast, multiplier + 1)
  12. } else if cast, ok := el.(int); ok {
  13. sum += cast
  14. }
  15. }
  16. return sum * multiplier
  17. }

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:

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

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

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

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

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

  1. func helper(array SpecialArray, multiplier int) int {
  2. sum := 0
  3. for _, v := range array {
  4. switch v := v.(type) {
  5. case int:
  6. sum += v
  7. case SpecialArray:
  8. sum += helper(v, multiplier+1)
  9. default:
  10. log.Fatalf("未知类型:%T", v)
  11. }
  12. }
  13. return sum * multiplier
  14. }

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

英文:

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

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

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

  1. func helper(array SpecialArray, multiplier int) int {
  2. sum := 0
  3. for _, v := range array {
  4. switch v := v.(type) {
  5. case int:
  6. sum += v
  7. case SpecialArray:
  8. sum += helper(v, multiplier+1)
  9. default:
  10. log.Fatalf("Unknown type: %T", v)
  11. }
  12. }
  13. return sum * multiplier
  14. }

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:

确定