-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinlist.py
More file actions
37 lines (31 loc) · 1.09 KB
/
inlist.py
File metadata and controls
37 lines (31 loc) · 1.09 KB
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
37
from list_fns import load_words_append as load_words
# precondition: t is sorted list of values, target is value to be found
def in_bisect(t, target):
# verify we haven't run out of words to check
if len(t) == 0:
return False
# wordlist not empty; so check middle word
index = len(t) // 2
if target == t[index]:
# target word found!
return True
# word not found; recurse if there are more words to check
if target < t[index]:
# check first half of wordlist
return in_bisect(t[:index], target)
else:
# check second half of wordlist
return in_bisect(t[index + 1:], target)
def main():
t = load_words('words.txt')
t.sort()
target = input("Give me a word ('get me out!' to quit): ")
while target != 'get me out!':
if in_bisect(t, target):
print(f"Found it! '{target}' is a valid word.")
else:
print(f"Sorry, '{target}' is not a word!")
target = input("Give me a word ('get me out!' to quit): ")
print("Have a good day!")
if __name__ == '__main__':
main()