在Go语言中使用递归解决编程问题:Product Sum(求积和)

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

Recursion in Go for coding question: Product Sum

问题

我一直在使用Go语言练习编码面试题,其中一个例子是关于Product Sum的。基本上,你需要接收一个嵌套数组,并返回它的乘积和。

例如:[1,3,[2,[5],-3],7] = 1 + 3 + 2*(2-3) + 3*(5) + 7 = 24

这也可以等于:1 + 3 + 2*(2) + 2*(-3) + 3*5 + 7 = 24

然而,当我尝试用代码实现时,我只能得到第一个例子的结果。

如果我将sum += cast改为sum += cast * multiplier,并将return sum * multiplier改为return sum,那么函数的结果就不符合预期了。我已经尝试在这个问题上进行递归堆栈的调试,但仍然感到困惑。

英文:

I've been practicing coding interview questions with this current example of Product Sum with Go. Basically, you need to take a nested array and return it's product sum.

Example: [1,3,[2,[5],-3],7] = 1 + 3 + 2*(2-3) + 3*(5) + 7 = 24

Which should also be equal to: 1 + 3 + 2*(2) + 2*(-3) + 3*5 + 7 = 24

However, when I try implementing this in code I can only get the first example.

func ProductSum(array []interface{}) int {
	sum := productSum(array, 1)
	fmt.Println(sum)
	return sum
}

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

If I change sum += cast to sum += cast * multiplier, and change return sum * multiplier to return sum - then the function doesn't work as expected. I've tried working through the recursive stack on this but am still confused.

答案1

得分: 0

根据你的代码,"array notation" 应该转换为以下方程式:

[1,3,[2,[5],-3],7] = 1*(1 + 3 + 2*(2 + 3 * (5) - 3) + 7) = 1 + 3 + 2*(2 - 3 + 15) + 7 = 11 + 14 * 2 = 39

或者

[1,3,[2,[5],-3],7] = 1*(1 + 3 + 2*(2 + 3 * (5) - 3) + 7) = 1 + 3 + 2*(-1) + 6 * 5 + 7 = 11 - 2 + 30 = 39

根据你的解释,每个嵌套级别增加一个乘数。

你能粘贴一下这个练习的确切内容吗?

编辑:

要得到结果为24,你的代码应该像这样:

func ProductSum(array []interface{}) int {
	sum := productSum(array, 1)
	fmt.Println(sum)
	return sum
}

func productSum(array []interface{}, multiplier int) int {
	sum := 0
	for _, el := range array {
		if cast, ok := el.([]interface{}); ok {
			sum += productSum(cast, multiplier+1)
		} else if cast, ok := el.(int); ok {
            // 乘数仅应用于当前切片中的整数。
			sum += multiplier * cast
		}
	}
	return sum
}

数组的和总是乘以其嵌套级别。

英文:

According to your code the "array notation" should be transformed to following equation:

[1,3,[2,[5],-3],7] = 1*(1 + 3 + 2*(2 + 3 * (5) - 3) + 7) = 1 + 3 + 2*(2 - 3 + 15) + 7 = 11 + 14 * 2 = 39

or

[1,3,[2,[5],-3],7] = 1*(1 + 3 + 2*(2 + 3 * (5) - 3) + 7) = 1 + 3 + 2*(-1) + 6 * 5 + 7 = 11 - 2 + 30 = 39

In your interpretation each level of nesting increases the multiplier by one.

Could you paste the exact content of this exercise?

Edit:

To get 24 in result your code should look like this:

func ProductSum(array []interface{}) int {
	sum := productSum(array, 1)
	fmt.Println(sum)
	return sum
}

func productSum(array []interface{}, multiplier int) int {
	sum := 0
	for _, el := range array {
		if cast, ok := el.([]interface{}); ok {
			sum += productSum(cast, multiplier+1)
		} else if cast, ok := el.(int); ok {
            // Multiplier is applied only to integers in current slice.
			sum += multiplier * cast
		}
	}
	return sum
}

The sum of an array is always multiplied by the its level of nesting.

huangapple
  • 本文由 发表于 2021年8月2日 03:12:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/68613593.html
匿名

发表评论

匿名网友

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

确定