2 回答

TA貢獻1850條經驗 獲得超11個贊
程序中有多個錯誤。
您必須將 *args 和 **args 放在位置和關鍵字參數之后。
假設您已經修改了函數定義?,F在,它將數組轉換為元組,該元組將按照您的 algo.it 將列表轉換為列表元組也不起作用。
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貢獻1780條經驗 獲得超1個贊
更改此
element = BinarySearch(A, searchKey, arraySize)
自
element = BinarySearch(A, key=searchKey, size=arraySize)
添加回答
舉報