-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearch.py
More file actions
36 lines (31 loc) · 991 Bytes
/
BinarySearch.py
File metadata and controls
36 lines (31 loc) · 991 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from arrayutils import arrayContent
from time import time
def find(array,word):
return rec_find(array, word, 0, len(array))
def rec_find(array, word, start, end):
if end == start:
return False
mid = start + int((end - start)/2)
check = array[mid]
if check == word:
return True
elif check > word:
return rec_find(array, word, start, mid)
elif check < word:
return rec_find(array, word, mid + 1, end)
def main():
with open('twl06.txt')as f:
lexicon = f.read().splitlines()
testwords = ['aardvark','drywall','offramp','zygote','ziop']
for testword in testwords:
start = time()
iscontained = arrayContent(lexicon,testword)
end = time()
print(testword,iscontained,end-start)
for testword in testwords:
start = time()
iscontained = find(lexicon,testword)
end = time()
print(testword,iscontained,end-start)
if __name__ == "__main__":
main()