How do I call the binary search 10 times?

#I want to call the binary search 10 times (make the function run 10 times). Im supposed to that and then make a graph out of the result, which I know how to do. I just cant make the function run 10 times to get the information for the graph.

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")

>Solution :

To run some code a specific number of times, use a for-loop on a range:

for _ in range(10):
    binary_search(params)

Leave a Reply