You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助构建简易递归函数:实现数组特定加权累加计算

Recursive Solution for Your Custom Sum Calculation

Got it, let's break down this problem step by step so you can build that recursive function easily. First, let's confirm we're aligned on the pattern you described:

For SUM_k (using the first k+1 elements from your array, where G0 is the first element, G1 the second, etc.), each term Gi has a coefficient of (k - i + 1). For example:

  • SUM_0 = 1*G0
  • SUM_1 = 1*G1 + 2*G0
  • SUM_2 = 1*G2 + 2*G1 + 3*G0
  • SUM_3 = 1*G3 + 2*G2 + 3*G1 + 4*G0

Key Recursive Relationship

To build a recursive function, we need to link SUM_k to SUM_{k-1}. Let's work through the math:

  • SUM_{k-1} = 1*G_{k-1} + 2*G_{k-2} + ... + k*G0
  • SUM_k = 1*Gk + 2*G_{k-1} + 3*G_{k-2} + ... + (k+1)*G0

You can rewrite SUM_k as:
SUM_k = Gk + (SUM_{k-1} + (G0 + G1 + ... + G_{k-1}))

We also need to track the running total of G0 + G1 + ... + Gk (let's call this the prefix sum) to make the recursion work. Here's how the pieces fit:

  1. Base Case: When k=0 (only the first element), SUM_0 = G0 and the prefix sum is also G0.
  2. Recursive Step: For any k > 0, calculate SUM_k using the previous sum, previous prefix sum, and the current element Gk. Then update the prefix sum for the next iteration.

Python Recursive Implementation

Here's a clean, working example. We'll use a helper function to track both the sum and prefix sum, then a wrapper function to simplify usage:

def _recursive_helper(nums, k):
    # Base case: k=0, only the first element
    if k == 0:
        return nums[0], nums[0]
    # Get results from the previous step
    prev_sum, prev_prefix = _recursive_helper(nums, k-1)
    # Calculate current sum and prefix sum using our formula
    current_sum = nums[k] + prev_sum + prev_prefix
    current_prefix = prev_prefix + nums[k]
    return current_sum, current_prefix

def calculate_custom_sum(nums, k):
    # Validate input to avoid errors
    if not isinstance(k, int) or k < 0 or k >= len(nums):
        raise ValueError(f"k must be an integer between 0 and {len(nums)-1}")
    # Call the helper and return only the sum result
    final_sum, _ = _recursive_helper(nums, k)
    return final_sum

Test It Out

Let's verify with a small test array to ensure it works as expected:

# Test array: G0=1, G1=2, G2=3, G3=4
test_array = [1, 2, 3, 4]

print(calculate_custom_sum(test_array, 0))  # Output: 1 (matches SUM_0=G0)
print(calculate_custom_sum(test_array, 1))  # Output: 4 (matches SUM_1=2 + 2*1)
print(calculate_custom_sum(test_array, 2))  # Output: 10 (matches SUM_2=3 + 2*2 +3*1)
print(calculate_custom_sum(test_array, 3))  # Output: 20 (matches SUM_3=4 +2*3 +3*2 +4*1)

Notes for Your 93-Element Array

Since your array has 93 elements, the maximum k you'll use is 92 (since we start counting from 0). Python's default recursion depth limit is 1000, so you won't run into stack overflow issues here.

内容的提问来源于stack exchange,提问作者Julia Gorman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:53:05