我正在编写一个使用二进制搜索的程序来查找两个列表的交叉索引。我已经编写了整个程序,但是当我尝试返回findCrossoverIndexHelper(x,y,0,len(y))时,我遇到了一个空白断言错误。如果有人能指出断言错误在哪里,或者我如何追溯错误,我将不胜感激。
谢谢你的帮助。
#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):
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, 0,len(y))
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
AssertionError
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-257-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
<ipython-input-256-d93a1b9d946e> in findCrossoverIndex(x, y)
6 n = len(x)
7 assert(x[n-1] < y[n-1]) # Note: this automatically ensures n >= 2 why?
----> 8 return findCrossoverIndexHelper(x, y, 0,len(y))
<ipython-input-255-3ece5541426f> in findCrossoverIndexHelper(x, y, left, right)
9 assert(left >= 0)
10 assert(left <= right-1)
---> 11 assert(right < len(x))
12 # Here is the key property we would like to maintain.
13 assert(x[left] > y[left])
AssertionError:
发布于 2021-08-30 11:35:09
出现空白断言错误,因为默认的python断言需要第二个参数assert <Boolean> [<optional Error string>]
。
但是,您可以在您提供的回溯中看到断言错误:
---> 11 assert(right < len(x))
如果你使用pytest之类的东西,它会秘密地重写你的断言,使其更具信息性。
https://stackoverflow.com/questions/68989443
复制相似问题