From 5af81858bae61edfa9669eebecce980a8657a9cc Mon Sep 17 00:00:00 2001 From: BellCodeEditor <bellcode_dev@bell.ai> Date: Fri, 15 Mar 2024 19:47:29 +0800 Subject: [PATCH] save project --- my_search.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/my_search.py b/my_search.py index 0aaea96..f9c818c 100644 --- a/my_search.py +++ b/my_search.py @@ -3,20 +3,19 @@ import random alist = [] for i in range(1, 101): alist.append(i) -num = +num =random.choice(alist) # 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置 -def binary_search(alist,num_list): +def binary_search(alist,num): low=0 high=len(alist)-1 - num= - while low<high: + while low<=high: mid=(low+high)//2 - gues=alist[mid] - print(num,"的索引是",mid) - break + guess=alist[mid] + if guess==num: + return mid elif guess<num: low=mid+1 else: high=mid-1 - +print(binary_search(alist,num)) -- libgit2 0.25.0