首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何调用二进制搜索10次?

如何调用二进制搜索10次?
EN

Stack Overflow用户
提问于 2022-11-07 22:18:22
回答 1查看 21关注 0票数 1

#我想调用二进制搜索10次(使函数运行10次)。我应该这样做,然后根据结果做一个图表,我知道该怎么做。我只是不能让函数运行10次来获取图形的信息。

代码语言:javascript
运行
复制
import matplotlib.pyplot as plt
import random
import operator
import sys

def binary_search(rodun, gildi):
    lo, hi = 0, len(rodun) - 1
    while lo <= hi:
        mid = (lo + hi) // 2
        if rodun[mid][1] < gildi:         
            lo = mid + 1
        elif gildi < rodun[mid][1]:      
            hi = mid - 1
        else:
            return mid

def graph(x_as, y_as):
  try:
    plt.close()
    fig = plt.figure(figsize=(8,4))
    x_as = fig.add_subplot(111)
    x_as.bar(x_as, y_as)
    plt.x_aslim(1,11)
    plt.y_aslim(-1,11)
    plt.grid()
    plt.show()
    pass
  except:
    print("Úps!", sys.exc_info()[0], "occurred.")
  return

def main():
    x_as=[]
    y_as=[]
    y1= {'the':3460,'and':3192,'to':2384, 'of':1797, 'that': 1389,'was':1353,'a': 1253,
         'he': 1157,'in':995,'his':944}
    sorted_by_value_y1 = sorted(y1.items(), key=operator.itemgetter(1))      
    
    graph(x_as, y_as)
    
if __name__== "__main__":
    main()
else:
    print("Er inporrterað. Ekki þetta main fall")
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-07 22:22:43

若要运行某些特定次数的代码,请在范围上使用for-循环:

代码语言:javascript
运行
复制
for _ in range(10):
    binary_search(params)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74353547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档