Reputation: 49
I have a programm, which uses binary search. And in the end, i need to print a count of loops. How it would be better to do?
import re
def binarySearch(sumList, whattofind):
a=0
if len(sumList) == 0:
return False
else:
midpoint = len(sumList)/2
if sumList[midpoint]==whattofind:
a=a+1
print(a)
return True
else:
if whattofind<sumList[midpoint]:
a+=1
return binarySearch(sumList[:midpoint],whattofind)
else:
a+=1
return binarySearch(sumList[midpoint+1:],whattofind)
print(a)
result = re.findall(r'\w\w', open("text.txt","r").read())
sumList=[]
for line in result:
sumList.append(ord(line[0])+ord(line[1]))
sumList.sort()
whattofind=int(input('Enter number: '))
print (sumList)
print(binarySearch(sumList, whattofind))
Upvotes: 0
Views: 1633
Reputation: 829
do the following
count = 0
def binarySearch(sumList, whattofind):
global count
count += 1
and at the last line of code just print value of count
Upvotes: 1