缺少2个必需的位置参数:“left”和“right”

2024-09-29 17:15:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个程序,使用二进制搜索查找两个列表的交叉索引。我已经编写了helper函数,现在正在编写调用helper函数的函数。我得到一个类型错误:findCrossoverIndex()缺少2个必需的位置参数:“left”和“right”。有人能解释一下如何创建两个必需的位置参数吗

谢谢,

#First write a "helper" function with two extra parameters
# left, right that describes the search region as shown below
def findCrossoverIndexHelper(x, y, left, right):
    # Note: Output index i such that 
    #         left <= i <= right
    #         x[i] <= y[i]
    # First, Write down our invariants as assertions here
    assert(len(x) == len(y))
    assert(left >= 0)
    assert(left <= right-1)
    assert(right < len(x))
    # Here is the key property we would like to maintain.
    assert(x[left] > y[left])
    assert(x[right] < y[right])
    
    mid = (left+right)//2
    # if middle index = left then we found crossover
    if(mid == left):
        return mid
    # if middle index elements has x > y
    # then we need to change left to mid
    if(x[mid] >= y[mid]):
        return findCrossoverIndexHelper(x, y, mid, right)
    # if middle index elements has x < y
    # then we need to change right to mid
    elif (x[mid] < y[mid]):
        return findCrossoverIndexHelper(x, y, left, mid)
    

#Define the function findCrossoverIndex that will 
# call the helper function findCrossoverIndexHelper
def findCrossoverIndex(x, y,left,right):
    assert(len(x) == len(y))
    assert(x[0] > y[0])
    n = len(x)
    assert(x[n-1] < y[n-1]) # Note: this automatically ensures n >= 2 why?
    return findCrossoverIndexHelper(x, y, left, right)

# BEGIN TEST CASES
j1 = findCrossoverIndex([0, 1, 2, 3, 4, 5, 6, 7], [-2, 0, 4, 5, 6, 7, 8, 9])
print('j1 = %d' % j1)
assert j1 == 1, "Test Case # 1 Failed"

j2 = findCrossoverIndex([0, 1, 2, 3, 4, 5, 6, 7], [-2, 0, 4, 4.2, 4.3, 4.5, 8, 9])
print('j2 = %d' % j2)
assert j2 == 1 or j2 == 5, "Test Case # 2 Failed"

j3 = findCrossoverIndex([0, 1], [-10, 10])
print('j3 = %d' % j3)
assert j3 == 0, "Test Case # 3 failed"

j4 = findCrossoverIndex([0,1, 2, 3], [-10, -9, -8, 5])
print('j4 = %d' % j4)
assert j4 == 2, "Test Case # 4 failed"

print('Congratulations: all test cases passed - 10 points')
#END TEST CASES
TypeError: findCrossoverIndex() missing 2 required positional arguments: 'left' and 'right'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-186-87cacde37648> in <module>
      1 # BEGIN TEST CASES
----> 2 j1 = findCrossoverIndex([0, 1, 2, 3, 4, 5, 6, 7], [-2, 0, 4, 5, 6, 7, 8, 9])
      3 print('j1 = %d' % j1)
      4 assert j1 == 1, "Test Case # 1 Failed"
      5 

TypeError: findCrossoverIndex() missing 2 required positional arguments: 'left' and 'right'

Tags: totestrighthelperlenifassertleft
1条回答
网友
1楼 · 发布于 2024-09-29 17:15:06

您已经将findCrossoverIndex定义为findCrossoverIndex(x, y,left,right){}、yleftright是需要传递到函数中的4个必需参数j1 = findCrossoverIndex([0, 1, 2, 3, 4, 5, 6, 7], [-2, 0, 4, 5, 6, 7, 8, 9])传递两个列表xy,您仍然缺少leftright

相关问题 更多 >

    热门问题