我正在尝试使用迭代函数和递归函数来计算螺旋第 n 个臂的 x 坐标。但是,我没有从代码中得到预期的输出。我编写了两个函数:spiral_iterative 和spiral_recursive,它们应该根据输入返回特定值。但是,在某些情况下输出不正确。
问题: 这是我所期待的描述:
测试案例1: 输入: spiral_iterative(0, 8, 1) 和 spiral_recursive(0, 8, 1) 预期输出:4.0
测试案例2: 输入: spiral_iterative(0, 8, 2) 和 spiral_recursive(0, 8, 2) 预期输出:6.0
测试案例3: 输入: spiral_iterative(0, 8, 3) 和 spiral_recursive(0, 8, 3) 预期输出:5.0
测试用例4(大n): 输入: spiral_iterative(0, 16, 5) 和 spiral_recursive(0, 16, 5) 预期输出:11.0
其他详细信息: 我正在使用 Python 3.x。 函数背后的逻辑似乎是基于每次迭代/递归调整左右值以找到中点。 谢谢您的帮助!
但是输出并不如预期。
def spiral_iterative(left, right, n):
"""
An iterative function to compute the x-coordinate of the nth arm of the spiral.
Parameters:
left: integer
right: integer
n: integer
Return:
result: float
"""
mid = (left + right) / 2
for i in range(3, n):
mid = (left + right) / 2
left = mid
mid = (left + right) / 2
right = mid
return mid
def spiral_recursive(left, right, n):
"""
A recursive function to compute the x-coordinate of the nth arm of the spiral.
Arguments:
left: integer
right: integer
n: integer
Return:
result: float
"""
if n < 1:
return left
else:
mid = (left + right) / 2
if n % 2 == 0:
left = mid
else:
right = mid
return spiral_recursive(left, right, n - 1)
# Test Case 1
print(spiral_iterative(0, 8, 1)) # Expected: 4.0
print(spiral_recursive(0, 8, 1)) # Expected: 4.0
# Test Case 2
print(spiral_iterative(0, 8, 2)) # Expected: 6.0
print(spiral_recursive(0, 8, 2)) # Expected: 6.0
# Test Case 3
print(spiral_iterative(0, 8, 3)) # Expected: 5.0
print(spiral_recursive(0, 8, 3)) # Expected: 5.0
# Test Case 4 (Large n)
print(spiral_iterative(0, 16, 5)) # Expected: 11.0
print(spiral_recursive(0, 16, 5)) # Expected: 11.0
我的期望: 对于spiral_iterative(0, 8, 1),我期望 4.0。 对于spiral_recursive(0, 8, 1),我预计为4.0。 我预计其他测试用例也会有类似的输出。 怎么了: 输出不正确并且与预期值不匹配。迭代和递归函数似乎没有按预期运行。有人可以帮我找出可能导致代码中问题的原因吗?
这里,螺旋计算中每个步骤的循环恰好运行
n
次。以前,循环从 i=3
开始一直到 n-1
,因此 n=1
和 n=2
不会执行。对于 n=3
及更高版本,left
、mid
和 right
在一次迭代中错误地更新了多次。
def spiral_iterative(left, right, n):
for step in range(1, n + 1):
mid = (left + right) / 2
left, right = (mid, right) if step % 2 == 1 else (left, mid)
return mid
def spiral_recursive(left, right, n):
if n <= 0:
raise ValueError("Step number 'n' must be a positive integer.")
def helper(current_left, current_right, current_step):
mid = (current_left + current_right) / 2
if current_step == n:
return mid
if current_step % 2 == 1:
# Odd step: move to the right half by updating the left boundary.
return helper(mid, current_right, current_step + 1)
else:
# Even step: move to the left half by updating the right boundary.
return helper(current_left, mid, current_step + 1)
return helper(left, right, 1)
测试用例:
Test Case 1:
spiral_iterative(0, 8, 1): 4.0 (Expected: 4.0)
spiral_recursive(0, 8, 1): 4.0 (Expected: 4.0)
Test Case 2:
spiral_iterative(0, 8, 2): 6.0 (Expected: 6.0)
spiral_recursive(0, 8, 2): 6.0 (Expected: 6.0)
Test Case 3:
spiral_iterative(0, 8, 3): 5.0 (Expected: 5.0)
spiral_recursive(0, 8, 3): 5.0 (Expected: 5.0)
Test Case 4:
spiral_iterative(0, 16, 4): 11.0 (Expected: 11.0)
spiral_recursive(0, 16, 4): 11.0 (Expected: 11.0)
Test Case 5:
spiral_iterative(0, 16, 5): 10.5 (Expected: 10.5)
spiral_recursive(0, 16, 5): 10.5 (Expected: 10.5)
我的 4(和 5)测试用例略有不同。我不确定这是否是因为我误解了问题,或者之前的预期输出是否有错误。