Commit ce23a357 by BellCodeEditor

save project

parent 0f2cab67
Showing with 33 additions and 7 deletions
import random from random import randint as rdt, choice as chc
alist = [] num_list = []
for i in range(1, 101): step = rdt(1, 3)
alist.append(i)
num = random.choice(alist) for i in range(1, 101, step):
num_list.append(i)
guess_num = chc(num_list)
# 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置 # 请完善二分查找函数binary_search(),查找出num在列表alist里面的索引位置
def binary_search(alist,num): def binary_search(alist,num):
\ No newline at end of file '''
binary search a num.
alist is want search list,
num is want search number.
'''
low,high = 0,len(alist) - 1
while low <= high:
mid = (low + high)
guess_num = num_list[mid]
if guess_num == num:
return str(mid)
elif guess_num < num:
low = mid + 1
elif guess_num > num:
high = mid - 1
return None
result = binary_search(num_list,guess_num)
guess_num = str(guess_num)
print("要猜的数字:%s"%(guess_num))
print("它在列表里的索引是:%s"%(result))
print("列表是:%a"%(num_list))
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment