python --二分法学习
def found_number(need_vaule,l): print(l) mid_index=len(l) // 2 mid_value=l[mid_index] print("mid_value is %s"%(mid_value)) if mid_value > need_vaule: l=l[:mid_index] print('need to find1') found_number(need_vaule,l) elif mid_value < need_vaule: l=l[mid_index+1:] print("need to find 2") found_number(need_vaule,l) else: print("find it") l=[1,2,4,6,8,10,11,44,66,99] found_number(44,l)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/usr/local/bin/python3.8 /Users/futantan/PycharmProjects/S14/函数的学习/二分法.py [1, 2, 4, 6, 8, 10, 11, 44, 66, 99] mid_value is 10 need to find 2 [11, 44, 66, 99] mid_value is 66 need to find1 [11, 44] mid_value is 44 find it Process finished with exit code 0
每天进步一点点~~