-
Notifications
You must be signed in to change notification settings - Fork 1
/
A4_Q2_V9.py
450 lines (394 loc) · 11.8 KB
/
A4_Q2_V9.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
##Testing out github
##hello
<<<<<<< HEAD:A4_Q2_V9.py
##omg I can't
=======
>>>>>>> 7c0bb1dd4c5e84a964b6550307b7cf3fc0fe5baa:python-singly-linked-list-sorting-algos/A4_Q2_V9.py
'''A4_Q2_V9 testing all of the sorting methods'''
class Person:
def __init__(self, name, personality):
self.name = name
self.personality = personality
def getPersonality(self):
return self.personality
def __str__(self):
return "Name - " + self.name + "||Personality - " + str(self.personality)
class Chair:
def __init__(self, data, next=None):
self.data = data
self.next = next
def set_data(self, data):
self.data = data
def get_data(self):
return self.data
def set_next(self, next):
self.next = next
def get_next(self):
return self.next
def __str__(self):
return "chair: " + self.data.name + " || " + str(self.data.personality)
class Classroom:
def __init__(self):
self.head = None
self.last = None
self.length = 0
def insert(self, person):
'''inserts a person to the head of the classroom'''
temp = Chair(person)
if person == None:
'''case 1: no person is added'''
print("There is no person to insert!")
if self.head == None:
'''case 2: classroom is empty'''
self.head = temp
self.last = temp
self.length = 1
else:
temp.set_next(self.head)
self.head = temp
self.length += 1
def add(self, person):
'''Adds the person to the correct position in the list'''
temp = Chair(person)
if self.head == None:
'''case 1: empty list'''
self.head = temp
self.last = temp
elif temp.get_data().personality < self.head.get_data().personality:
temp.set_next(self.head)
self.head = temp
elif temp.get_data().personality > self.last.get_data().personality:
self.last.set_next(temp)
self.last = temp
else:
current = self.head
next = self.head.get_next()
while next != None:
if temp.get_data().personality < next.get_data().personality:
temp.set_next(next)
current.set_next(temp)
break
current = next
next = current.get_next()
def contains(self, personality):
'''checks if personality is contained the list/classroom and returns a
boolean value'''
f = False
current = self.head
while current != None:
if current.get_data().personality == personality:
f = True
break
current = current.get_next()
return f
def remove(self, personality):
'''finds the person based on his/her personality rating and removes the
person from the list'''
if self.head == None:
'''case 1: empty classroom'''
print("There are no chairs in this classroom! Cannot remove anything")
if self.contains(personality) == False:
'''case 2: personality is not in the classroom'''
print(str(personality) + " is not in the list!")
if self.head.get_data().personality == personality:
self.head = self.head.get_next()
self.length -= 1
else:
prev = self.head
current = self.head.get_next()
while current != None:
if current.get_data().personality == personality:
prev.set_next(current.get_next())
current.set_next(None)
self.length -= 1
break
prev = current
current = prev.get_next()
def bubble_sort(self):
'''bubble sort the classroom'''
if self.head == None:
'''case 1: empty classroom'''
print("There's nothing to sort!")
if self.head == self.last:
'''case 2: only one chair in the classroom'''
print("There's only one element in the list!")
else:
sorted = False
count = 0
while not sorted:
sorted = True
prev = self.head
current = self.head.get_next()
while current != None:
if prev.get_data().personality > current.get_data().personality:
sorted = False
temp = prev.get_data()
prev.set_data(current.get_data())
current.set_data(temp)
count += 1
print("swap " + str(count) + ": " + self.return_list())
print(" ")
prev = current
current = current.get_next()
def insertion_sort(self):
'''insertion sort the classroom'''
if self.head == None:
'''case 1: empty classroom'''
print("There's nothing to sort!")
if self.head == self.last:
'''case 2: one chair in classroom'''
print("There's on one element in the list!")
else:
prev = self.head
current = self.head.get_next()
while current != None:
if prev.get_data().personality > current.get_data().personality:
print("head: " + str(self.head.get_data().personality))
print("prev: " + str(prev.get_data().personality))
print("current: " + str(current.get_data().personality))
self.print_list()
print(" ")
prev.set_next(current.get_next())
current_1 = self.head
next = self.head.get_next()
while current_1 != prev.get_next():
if current.get_data().personality < self.head.get_data().personality:
current.set_next(self.head)
self.head = current
break
elif current.get_data().personality < next.get_data().personality:
current.set_next(next)
current_1.set_next(current)
break
current_1 = next
next = current_1.get_next()
prev = current
current = current.get_next()
def find_chair(self, person):
'''finds the chair containing the person in the classroom -> returns the chair
used as a helper function to other sorting methods'''
found = False
current = self.head
while current != None:
if current.get_data() == person:
found = True
return current
break
current = current.get_next()
def print_list(self):
'''prints the list'''
a = []
current = self.head
while current != None:
a.append(current.get_data().name + " : " + str(current.get_data().personality))
current = current.get_next()
print(bcolors.OKGREEN + str(a) + bcolors.ENDC)
def return_list(self):
'''returns the list'''
a = []
current = self.head
while current != None:
a.append(current.get_data().name + " : " + str(current.get_data().personality))
current = current.get_next()
return(str(bcolors.OKGREEN + str(a) + bcolors.ENDC))
def swap(self, A, B):
'''swaps the data in two chairs'''
temp = A.get_data()
A.set_data(B.get_data())
B.set_data(temp)
def partition_chair(self, lo_person_chair, hi_person_chair):
'''partitions the list using the last person in the classroom as the pivot'''
pivot = hi_person_chair
print(bcolors.WARNING + "pivot: " + str(pivot.get_data()) + bcolors.ENDC)
hi_chair = hi_person_chair
print(bcolors.WARNING + "hi_chair: " + str(hi_chair.get_data()) + bcolors.ENDC)
current = lo_person_chair
print(bcolors.WARNING + "current: " + str(current.get_data()) + bcolors.ENDC)
switcher = current
while current != hi_chair:
if current.get_data().personality < pivot.get_data().personality:
print("current: " + str(current.get_data().personality))
print("pivot: " + str(pivot.get_data().personality))
self.print_list()
self.swap(switcher, current)
switcher = switcher.get_next()
print("current: " + str(current.get_data().personality))
print("pivot: " + str(pivot.get_data().personality))
self.print_list()
current = current.get_next()
self.swap(pivot, switcher)
print("final partition: " + self.return_list())
return switcher
def quick_sort(self, lo_person_chair, hi_person_chair):
print("quick sort was called!")
self.print_list()
if self.is_after_chair(lo_person_chair, hi_person_chair) == True:
m = self.partition_chair(lo_person_chair, hi_person_chair)
self.quick_sort(m.get_next(), self.last)
lower = self.find_before(self.head, self.last, m)
self.quick_sort(lo_person_chair, lower)
def find_before(self, first_chair, last_chair, person_chair):
'''find the chair before the person_chair'''
print("find_before was called")
current = first_chair
last = last_chair
personn = person_chair
print("current: " + current.get_data().name)
print("last: " + last.get_data().name)
#print(last.get_next().get_data().name)
while current != last.get_next():
if current.get_next() == personn:
print("before person was found: " + current.get_data().name)
return current
break
current = current.get_next()
return None
def is_after_chair(self, first_chair, last_chair):
'''boolean: returns True if the last_chair is after first_chair'''
print("is_after_chair was called")
if first_chair == last_chair:
found = False
elif first_chair == None or last_chair == None:
found = False
else:
current = first_chair
print(current.get_data().name)
lastt = last_chair
found = False
while current != None:
if lastt == current:
found = True
return True
break
current = current.get_next()
return found
def get_length(self):
return self.length
def radix_sort(self):
'''radix sort the classroom'''
if self.head == None:
print("The Classroom is empty!")
elif self.head == self.last:
print("There is only one person in the classroom")
else:
len_list = self.get_length()
modulus = 10
div = 1
while True:
new_list = [[],[],[],[],[],[],[],[],[],[]]
current = self.head
while current != None:
current_value = current.get_data().personality
least_digit = current_value % modulus
least_digit /= div
least_digit = int(least_digit)
new_list[least_digit].append(current)
current = current.get_next()
#self.print_list()
modulus = modulus * 10
div = div * 10
if len(new_list[0]) == len_list:
print("done")
break
self.head = None
self.last = None #clears the classroom
A = [str(a) for i in new_list for a in i]
print(A)
print(" ")
for x in reversed(new_list):
for y in reversed(x):
self.insert(y.get_data())
def clear(self):
self.head = None
self.last = None
def main():
##Testing Area##
Rebecca = Person("Rebecca", 50)
Samantha = Person("Samantha", 34)
Andrew = Person("Andrew", 89)
Charlie = Person("Charlie", 69)
Martha = Person("Martha", 2)
Alex = Person("Alex", 65)
Robert = Person("Robert", 3)
May = Person("May", 71)
print("===========TESTING AREA============")
B = Classroom()
B.insert(Samantha)
B.insert(Rebecca)
B.insert(Andrew)
B.insert(Martha)
B.insert(Charlie)
B.insert(Alex)
B.insert(Robert)
B.insert(May)
print("=========TESTING BUBBLE SORT=========")
B.print_list()
print("================START===============")
B.bubble_sort()
print("===========FINAL LIST===============")
B.print_list()
B.clear()
###########################################
print("========TESTING INSERTION SORT========")
B = Classroom()
B.insert(Samantha)
B.insert(Rebecca)
B.insert(Andrew)
B.insert(Martha)
B.insert(Charlie)
B.insert(Alex)
B.insert(Robert)
B.insert(May)
B.print_list()
print("================START===============")
B.insertion_sort()
print("===========FINAL LIST===============")
B.print_list()
B.clear()
###########################################
print("========TESTING QUICK SORT========")
B = Classroom()
B.insert(Samantha)
B.insert(Rebecca)
B.insert(Andrew)
B.insert(Martha)
B.insert(Charlie)
B.insert(Alex)
B.insert(Robert)
B.insert(May)
B.print_list()
print("================START===============")
B.quick_sort(B.head, B.last)
print("===========FINAL LIST===============")
B.print_list()
B.clear()
###########################################
print("========TESTING RADIX SORT========")
B = Classroom()
B.insert(Samantha)
B.insert(Rebecca)
B.insert(Andrew)
B.insert(Martha)
B.insert(Charlie)
B.insert(Alex)
B.insert(Robert)
B.insert(May)
B.print_list()
print("================START===============")
B.radix_sort()
print("===========FINAL LIST===============")
B.print_list()
if __name__ == "__main__":
main()
# print("head: " + B.head.get_data().name)
# print("last: " + B.last.get_data().name)
# print(B.get_length())