请问怎么用python写折半查找的程序?

请问怎么用python写折半查找的程序?,第1张

def find(array,value):

    start,end=0,len(array)-1

   

    while start<=end:

        mid=(start+end)//2

        if a[mid][0]==value:

            return mid

      咐局  elif a[mid][0]>value:

            end=mid-1

        else:

            start=mid+1

        

    return None

if __name__=="__main__"袜差:

    array=[ ['201801', '张三'], ['201822', 'Andy Lee'], ... ,['20189X','Austin Hu'] ],省略数据请自行补充

    studentno=input("请输入指定学号")

    index=find(array,studentno)

   告简皮 if index is None:

        print("未找到学号:%s"%studentno)

    else:print(array[index])

#!bin/python #-*- encoding: utf-8 -*- def counter(path, find, punctuation): infile = open(path, "r") lenth = len(find) count = [] for i in range(lenth): count.append(0) dat = infile.readline().strip("\n") while dat != '': dat = dat.split() for elemt in dat: elemt = elemt.strip(punctuation) #去除标点符号粗斗 if elemt in find: i = find.index(elemt) count[i] += 1 dat = infile.readline().strip("\n") infile.close() for i in range(lenth): print "%s:%d次" % (find[i],count[i]) if __name__ == "__main__": path = "PATH" find = ["hello", "袜凳手hi", "world"] punctuation = ''告嫌',.'":!?''' counter(path, find, punctuation)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12504902.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-25
下一篇 2023-05-25

发表评论

登录后才能评论

评论列表(0条)

保存