2 回答

TA貢獻(xiàn)1850條經(jīng)驗(yàn) 獲得超11個(gè)贊
程序中有多個(gè)錯(cuò)誤。
您必須將 *args 和 **args 放在位置和關(guān)鍵字參數(shù)之后。
假設(shè)您已經(jīng)修改了函數(shù)定義?,F(xiàn)在,它將數(shù)組轉(zhuǎn)換為元組,該元組將按照您的 algo.it 將列表轉(zhuǎn)換為列表元組也不起作用。
def BinarySearch( key, size,*args):
pass
[] -> ([], )
3.So,您只需要放置陣列部分。請參閱以下代碼。
# Binary Search
def BinarySearch(arr, key, size):
print(args)
low = 0
high = size - 1
while low <= high:
mid = (low + high) // 2
if key < args[mid]:
high = mid - 1
else:
if key > args[mid]:
low = mid + 1
else:
return mid + 1
return -1
arraySize = 10
A = [num * 2 for num in range(10)]
print("Numbers in array are : ", A)
searchKey = input("Enter integer search key : ")
element = BinarySearch(A, int(searchKey), arraySize)
if element != -1:
print("Found value in element : ", element)
else:
print("Value not found.")

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超1個(gè)贊
更改此
element = BinarySearch(A, searchKey, arraySize)
自
element = BinarySearch(A, key=searchKey, size=arraySize)
添加回答
舉報(bào)