-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy path6a PSG (search linear and binary).py
89 lines (78 loc) · 3.04 KB
/
6a PSG (search linear and binary).py
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#PySimple examples (v 3.9)
#Tony Crewe
#Oct 2018 MacOs
import FreeSimpleGUI as sg
sg.SetOptions(background_color = 'Grey',
element_background_color = 'Grey',
text_element_background_color = 'Grey',
font= ('Calibri', 14, 'bold'))
layout =[[sg.Text('Search Demo', font =('Calibri', 18, 'bold')), sg.ReadButton('Show Names')],
[sg.Text('',size = (14, 11),relief=sg.RELIEF_SOLID,font = ('Calibri', 12), background_color ='White',key = '_display1_'),
sg.Text('',size = (14, 11),relief=sg.RELIEF_SOLID,font = ('Calibri', 12), background_color ='White',key = '_display2_')],
[sg.Text('_'*35,font = ('Calibri', 16))],
[sg.InputText(size = (10,1), key = '_linear_'), sg.InputText(size = (11,1), key = '_binary_')],
[sg.ReadButton('Linear Search', size = (11,1)), sg.ReadButton('Binary Search', size = (11,1))],
]
window = sg.Window('Search Demo').Layout(layout)
#names for Demo, could be loaded from a file
names = ['Roberta', 'Kylie', 'Jenny', 'Helen',
'Andrea', 'Meredith','Deborah','Pauline',
'Belinda', 'Wendy']
sorted_names = ['Andrea','Belinda','Deborah','Helen',
'Jenny','Kylie','Meredith','Pauline',
'Roberta','Wendy']
#function to display list
def display_list(list, display):
names = ''
for l in list: #add list elements with new line
names = names + l + '\n'
window.FindElement(display).Update(names)
#Linear Search - no need for Ordered list
def linear_search():
l = names[:]
found = False
for l in l:
if l == value['_linear_']: #Check each value
found = True
window.FindElement('_display1_').Update('Linear search\n' + l + ' found.')
break
if not found:
window.FindElement('_display1_').Update(value['_linear_'] + ' was \nNot found')
#Binary Search - only works for ordered lists
def binary_search():
l = sorted_names[:]
lo = 0
hi = len(l)-1
#Start with found is False
found = False
while lo <= hi:
#Start in middle
mid = (lo + hi) //2
#get the value from the search box
if l[mid] == value['_binary_']:
window.FindElement('_display2_').Update('Binary search\n' + l[mid] + ' found.')
#If found display name and stop
found = True
break
elif l[mid] < value['_binary_']:
#Search in top half
lo = mid + 1
else:
#Search in lower half
hi = mid - 1
#If we get to end - display not found
if not found:
window.FindElement('_display2_').Update(value['_binary_'] + ' was \nNot found')
while True:
button, value = window.Read()
if button is not None:
#show names - unordered and sorted
if button == 'Show Names':
display_list(names,'_display1_')
display_list(sorted_names, '_display2_')
if button == 'Linear Search':
linear_search()
if button == 'Binary Search':
binary_search()
else:
break