-
Notifications
You must be signed in to change notification settings - Fork 0
/
all_code.py
570 lines (492 loc) · 22.5 KB
/
all_code.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
import math
import bisect
import StringIO
import collections
###################################################################
# Part (d)
# Description:
# Gradebook is a data structure that keeps track of every student
# and their grade information. The cool thing about Gradebook is that
# returning the k most average students takes only O(k) time, and
# updating a student's grade takes O(log(n) + k) time! Unfortunately,
# it hasn't been implemented yet.
#
# Hint:
# In your data structure, you will need to keep a running average of
# each student's grade. The way to do that is to keep track, for each
# student, of the total number of credits the student has taken so far,
# and the sum of his grades weighted by the number of credits.
# For instance, if a student takes a 12-unit and then a 6-unit class and
# gets a 5 then a 2, we keep track of (18, 72). SO we want the total number of credits, and then the weighted sum fo teh taol numbe rof credits. Then, the student's
# GPA is 72/18=4.
#
# TODO:
# Using your design in part (c), use "__init__" to define and initialize
# the data structures you will need, and then fill in the methods
# "update_grade", "average", and "middle" (descriptions below).
###################################################################
class Gradebook:
# TODO
def __init__(self, student_names, k):
# Define and initialize your data structure!
#make the
#Initate a list of student name
gpa = [0] * len(student_names)
n = len(student_names)
# Check if n is odd or even:
# Check if n is odd or even:
if n % 2 == 0:
if k %2 == 0:
upper = int(math.floor((n-k)/float((2))))
else:
upper = int(math.floor((n-k)/float((2))))+1 #7 = 2 + 2 +3 (15/2 = 2
else:
upper = int(math.floor((n-k)/float((2)))) #7 = 2 + 2 +3 (15/2 = 2
lower = n - (upper + k) # =k3+5 = 8 = 2 10 - (2+4) = 3
values = [(0,0,0)] * len(student_names)
self.all_students_scores = dict(zip(student_names, values))
self.lowerStudents = Max_Heap(student_names[:lower], gpa[:lower])
self.middleStudentNames = student_names[lower:k+lower] #this is giving the two people.
self.upperStudents = Min_Heap(student_names[k+lower:n], gpa[k+lower:n])
def sort(self, index):
curr_index = index
k = len(self.middleStudentNames)
i = index
# the element at i might be out of place
while True:
current_nm = self.middleStudentNames[i]
#getting the current name
current_el = self.all_students_scores[current_nm][2]
if i<k-1 and current_el > self.all_students_scores[self.middleStudentNames[i+1]][2]:
# swap to the right
self.middleStudentNames[i] = self.middleStudentNames[i+1]
self.middleStudentNames[i+1] = current_nm
i += 1
elif i>0 and current_el < self.all_students_scores[self.middleStudentNames[i-1]][2]:
self.middleStudentNames[i] = self.middleStudentNames[i-1]
self.middleStudentNames[i-1] = current_nm
i -= 1
else:
break
return
def update_grade(self, student, credit, grade):
# Updates student with the new credit and grade information, and
# makes sure "middle()" still returns the k most average students
# in O(k) time. Does not need to return anything.
#updates studnet iwth the new credit and grade informaiton.
#here, you want to simply update these ones.
#print("at the beginning of update_grade")
# self.lowerStudents.show_tree()
#print(self.middle())
# self.upperStudents.show_tree()
cm = credit*grade
current_cm, current_credits, oldGPA = self.all_students_scores[student]
newGPA = 0
new_entry = ( current_cm+cm, current_credits+ credit, newGPA)
self.all_students_scores[student] = new_entry
newGPA = self.average(student)
new_entry = ( current_cm+cm, current_credits+ credit, newGPA)
self.all_students_scores[student] = new_entry
#computing the GPA to insert
k = len(self.middleStudentNames)
#Case I: If student is in the lower students - max heap.
if student in self.lowerStudents.key_to_index:
#Modify the student's GPA with the new GPA.
min_k = self.all_students_scores[self.middleStudentNames[0]][2]
##print("min_k is ")
##print(min_k)
self.lowerStudents.max_heap_modify(student, newGPA)
#if the maximum of the lower quartile is more than the minimum of the k_array
if self.lowerStudents.data[0] > min_k:
#print("Swapping the lower with the middle")
#get the names to swap.
k_student_name = self.middleStudentNames[0]
former_max_student_name = self.lowerStudents.keys[0]
self.middleStudentNames[0] = former_max_student_name
#k_array --> lower Students
self.sort(0)
self.lowerStudents.extract_max()
self.lowerStudents.insert_key(k_student_name, min_k)
max_k = self.all_students_scores[self.middleStudentNames[k-1]][2]
if max_k> self.upperStudents.data[0]:
#print("Swapping upper with middle")
max_k_student_name = self.middleStudentNames[k-1]
former_min_student_name = self.upperStudents.keys[0]
#upperStudents --> k_array
self.middleStudentNames[k-1] = former_min_student_name
self.sort(k-1) #resort array.
#from k_array --> upperStudents
self.upperStudents.extract_min()
self.upperStudents.insert_key(max_k_student_name, max_k) #should be inserte dinot the minmimum heap.
elif student in self.middleStudentNames:
#get where the studnet is in the gpa list.
index = self.middleStudentNames.index(student)
self.sort(index)
max_k = self.all_students_scores[self.middleStudentNames[k-1]][2]
##print("computed average")
##print(max_k)
if self.upperStudents.data[0] < max_k:
#print("Swappin gupper with middle")
max_k_student_name = self.middleStudentNames[k-1] #k_array and middleStudents length should be the same.
former_min_student_name = self.upperStudents.keys[0]
self.middleStudentNames[k-1] = former_min_student_name
#now insert into the min_heap
self.sort(k-1)
self.upperStudents.extract_min()
self.upperStudents.insert_key(max_k_student_name, max_k) #should be inserte dinot the minmimum heap.
min_k = self.all_students_scores[self.middleStudentNames[0]][2]
##print("computed minimum of k")
##print(min_k)
if min_k < self.lowerStudents.data[0]:
#print("Swapping lower iwth middle")
k_student_name = self.middleStudentNames[0] #shoudl be the same index.
former_max_student_name = self.lowerStudents.keys[0]
# ###print("swapping" + k_student_name+" with "+ former_max_student_name)
self.middleStudentNames[0] = former_max_student_name
self.sort(0)
#doing the otehrway swap.
self.lowerStudents.extract_max() #pop from teh lwoer students
self.lowerStudents.insert_key(k_student_name, min_k) #insert_key
else:
#print("student is ")
#print(student)
#print("gpa is ")
#print(newGPA)
self.upperStudents.min_heap_modify(student, newGPA)
max_k = self.all_students_scores[self.middleStudentNames[k-1]][2]
if self.upperStudents.data[0] < max_k:
#print("Swapping the upper with the middle")
max_k_student_name = self.middleStudentNames[k-1] #k_array and middleStudents length should be the same.
former_min_student_name = self.upperStudents.keys[0]
self.middleStudentNames[k-1] = former_min_student_name
self.sort(k-1)
self.upperStudents.extract_min()
self.upperStudents.insert_key(max_k_student_name, max_k) #should be inserte dinot the minmimum heap.
min_k = self.all_students_scores[self.middleStudentNames[0]][2]
if min_k < self.lowerStudents.data[0]:
#print("swapping the lower with the middle")
k_student_name = self.middleStudentNames[0] #shoudl be the same index.
former_max_student_name = self.lowerStudents.keys[0]
# ###print("swapping" + k_student_name+" with "+ former_max_student_name)
self.middleStudentNames[0] = former_max_student_name
self.sort(0)
#doing the otehrway swap.
self.lowerStudents.extract_max() #pop from teh lwoer students
self.lowerStudents.insert_key(k_student_name, min_k) #insert_key
#print("at the end of this iteration of update_grade")
# self.lowerStudents.show_tree()
#print(self.middle())
# self.upperStudents.show_tree()
return
# TODO
def average(self, student):
# Return a single number representing the GPA for student
#you ahve the student name.
if self.all_students_scores[student][0] is 0 and self.all_students_scores[student][1] is 0:
return 0
return self.all_students_scores[student][0]/self.all_students_scores[student][1]
# TODO
def middle(self):
# Return the k most average students and their GPAs as a
# list of tuples, e.g. [(s1, g1),(s2,g2)]
#you wnt to cut away the part fo the aray hat are (n-k)/2 in both sides, and then iterate through and reutnr that suarray.
#okay, lets do this, so you need to comptue the GPA of this.
middle_array =[]
for index in range(0, len(self.middleStudentNames)):
student_name = self.middleStudentNames[index] #get the keys that match up with that index.
# ###print("the gpa")
# ###print( self.k_array[index] )
entry = (student_name, float(self.average(student_name)))
middle_array.append(entry)
return middle_array
###################################################################
# Part (a)
# Description:
# Max_Heap is a general implementation of a max-heap modified to accept
# (key, data) pairs. For instance, in the student GPA problem, the
# "key" would a student name and the "data" would be the student's GPA,
# e.g. ("Bob Dylan", 1)
#
# Implementation/Initialization Details:
# The keys (i.e. student names) are stored in a list called "self.keys"
# The data (i.e. student GPAs) are stored in a list called "self.data"
# Additionally, a dictionary called "self.key_to_index_mapping" keeps
# track of the index of the key in the array. For instance, if we had
# the following list of (key, data) pairs, which already satisfies the
# max-heap property:
#
# [("Ray Charles", 4), ("Bob Dylan", 1), ("Bob Marley", 3)]
#
# then, self.keys = ["Ray Charles", "Bob Dylan", "Bob Marley"]
# self.data = [4, 1, 3]
# self.key_to_index = {"Ray Charles": 0,
# "Bob Dylan": 1,
# "Bob Marley": 2}
# where 0, 1, 2 corresponds to the index in "self.keys" #so the sself.keys respond to the inde xin the slef. keys index.
#
# Provided Methods:
# All the methods presented in lecture and recitation are provided, with
# only slight changes to accomodate the (key, data) pair modification.
# In addition, we provide the method show_tree(self) so that you may
# ###print out what your heap looks like.
#
# TODO: Fill out the method "max_heap_modify(self, key, data)", which modifies,
# the data of "key" to the new "data" and restores the heap invariant.
# For instance, using the example above, we may call:
#
# heap.max_heap_modify("Bob Marley", 5)
#
# This should change Bob Marley's grade to 5, and then restore
# the heap invariant so that the data structure looks like this:
#
#so you want to ssrot the keys, and then sort the dakte, ,a but int he self.key_to_index youw ant to simply chagne the idnicews f where
#the keys rae lined up to adn not the order ot he anems ro tuples.
# self.keys = ["Bob Marley", "Bob Dylan", "Ray Charles"]
# self.data = [5, 1, 4] #so this is a max heap
# self.key_to_index = {"Ray Charles": 2,
# "Bob Dylan": 1,
# "Bob Marley: 0"}
# ARE STUDNET GPAs discrete.
###################################################################
class Max_Heap:
def __init__(self, keys, data):
try:
assert len(keys) == len(data)
self.keys = collections.deque(keys)
self.data = collections.deque(data)
self.key_to_index = dict(zip(self.keys, range(len(self.keys)))) #!
self.heapify()
except Exception as e:
pass
# TODO
def max_heap_modify(self, key, data):
curr_index = self.key_to_index[key]
curr_data = self.data[curr_index]
if data < curr_data:
self.data[curr_index] = data
self.max_heapify(curr_index)
if data > curr_data:
self.increase_key(curr_index, data)
#assert self.check_heap_invariant()
return
def maximum(self):
return (self.keys[0], self.data[0])
def extract_max(self):
if len(self.keys)<1:
raise Exception("No elements in heap!")
sm, gm = self.keys.popleft(), self.data.popleft()
del self.key_to_index[sm]
if len(self.keys) > 0:
s, g = self.keys.pop(), self.data.pop()
self.keys.appendleft(s)
self.data.appendleft(g)
self.key_to_index[s] = 0
self.max_heapify(0)
return (sm, gm)
def insert_key(self, k, d):
self.keys.append(k)
self.data.append(-float("inf"))
self.key_to_index[k] = len(self.keys)-1 #!
self.increase_key(len(self.keys)-1, d)
def max_heapify(self, i):
heap_size = len(self.keys)
l = i*2 + 1
r = i*2 + 2
largest = i
if l < heap_size and self.data[l] > self.data[i]:
largest = l
if r < heap_size and self.data[r] > self.data[largest]:
largest = r
if largest != i:
self.swap(largest, i)
self.max_heapify(largest)
def increase_key(self, i, key):
if key < self.data[i]:
raise Exception("New key is smaller than current key.")
self.data[i] = key
parent = (i-1)/2
while i > 0 and self.data[i] > self.data[parent]: #yyou want to get the self = self.data is more than kelf.data of the parents. SO
#I is the current key of the =actualy key, but the idnex oc that.
self.swap(i, parent)
i = parent
parent = (i-1)/2
def heapify(self):
for i in range(len(self.keys)/2)[::-1]:
self.max_heapify(i)
def swap(self, i1, i2):
self.key_to_index[self.keys[i1]] = i2 #!
self.key_to_index[self.keys[i2]] = i1 #!
self.data[i1], self.data[i2] = self.data[i2], self.data[i1]
self.keys[i1], self.keys[i2] = self.keys[i2], self.keys[i1]
# modified from https://pymotw.com/2/heapq/
# Displays the heap as a tree
def show_tree(self, total_width=60, fill=' '):
"""Pretty-###print a tree."""
output = StringIO.StringIO()
last_row = -1
for i, n in enumerate(self.keys):
if i:
row = int(math.floor(math.log(i+1, 2)))
else:
row = 0
if row != last_row:
output.write('\n')
columns = 2**row
col_width = int(math.floor((total_width * 1.0) / columns))
towrite = str(n) + " (" + str(self.data[i]) + ")"
output.write(str(towrite).center(col_width, fill))
last_row = row
#print(output.getvalue())
#print( '-' * total_width)
return ""
########################################################################
# The following methods are methods used for testing and may be ignored.
########################################################################
def check_heap_invariant(self):
n = len(self.data)
for i in range(n/2):
parent = self.data[i]
if parent < self.data[2*i+1]:
return False
if 2*i + 2 < n:
if parent < self.data[2*i+2]:
return False
return True
def check_student_index(self):
for s,i in self.key_to_index.iteritems():
if i >= len(self.keys):
return False
if self.keys[i] != s:
return False
return True
###################################################################
# Part (b)
# The implementation of Min_Heap is the same as Max_Heap, but modified
# to be a min-heap. Please refer to the description for Max_Heap.
###################################################################
class Min_Heap:
'''
keys: list of key values
data: list of data that corresponds to the key values
student_index: a dictionary that maps students names to their index in the
list representation of the heap
'''
def __init__(self, keys, data):
assert len(keys) == len(data)
self.keys = collections.deque(keys)
self.data = collections.deque(data)
self.key_to_index = dict(zip(self.keys, range(len(self.keys)))) #!
self.heapify()
# TODO
def min_heap_modify(self, key, data):
# self.show_tree()
curr_index = self.key_to_index[key]
curr_data = self.data[curr_index]
if data > curr_data:
self.data[curr_index] = data
self.min_heapify(curr_index)
if data < curr_data:
self.decrease_key(curr_index, data)
#
# assert self.check_heap_invariant()
return
def minimum(self):
return (self.keys[0], self.data[0])
def extract_min(self):
if len(self.keys)<1:
raise Exception("No elements in heap!")
sm, gm = self.keys.popleft(), self.data.popleft()
del self.key_to_index[sm]
if len(self.keys) > 0:
s, g = self.keys.pop(), self.data.pop()
self.keys.appendleft(s)
self.data.appendleft(g)
self.key_to_index[s] = 0
self.min_heapify(0)
return (sm, gm)
def insert_key(self, k, d):
self.keys.append(k)
self.data.append(float("inf"))
self.key_to_index[k] = len(self.keys)-1 #!
self.decrease_key(len(self.keys)-1, d)
def min_heapify(self, i):
heap_size = len(self.keys)
l = i*2 + 1
r = i*2 + 2
smallest = i
if l < heap_size and self.data[l] < self.data[i]:
smallest= l
if r < heap_size and self.data[r] < self.data[smallest]:
smallest = r
if smallest != i:
self.swap(smallest, i)
self.min_heapify(smallest)
def decrease_key(self, i, key):
if key > self.data[i]:
raise Exception("New key is larger than current key.")
self.data[i] = key
parent = (i-1)/2
while i > 0 and self.data[i] < self.data[parent]:
self.swap(i, parent)
i = parent
parent = (i-1)/2
def heapify(self):
for i in range(len(self.keys)/2)[::-1]:
self.min_heapify(i)
# Exchanges the students and GPAs at two indices
def swap(self, i1, i2):
self.key_to_index[self.keys[i1]] = i2 #!
self.key_to_index[self.keys[i2]] = i1 #!
self.data[i1], self.data[i2] = self.data[i2], self.data[i1]
self.keys[i1], self.keys[i2] = self.keys[i2], self.keys[i1]
# modified from https://pymotw.com/2/heapq/
# Displays the heap as a tree
def show_tree(self, total_width=60, fill=' '):
"""Pretty-###print a tree."""
output = StringIO.StringIO()
last_row = -1
for i, n in enumerate(self.keys):
if i:
row = int(math.floor(math.log(i+1, 2)))
else:
row = 0
if row != last_row:
output.write('\n')
columns = 2**row
col_width = int(math.floor((total_width * 1.0) / columns))
towrite = str(n) + " (" + str(self.data[i]) + ")"
output.write(str(towrite).center(col_width, fill))
last_row = row
#print(output.getvalue())
#print('-' * total_width)
###print
return ""
########################################################################
# The following methods are methods used for testing and may be ignored.
########################################################################
def check_heap_invariant(self):
n = len(self.data)
for i in range(n/2):
parent = self.data[i]
if parent > self.data[2*i+1]:
return False
if 2*i + 2 < n:
if parent > self.data[2*i+2]:
return False
return True
def check_student_index(self):
for s,i in self.key_to_index.iteritems():
if i >= len(self.keys):
return False
if self.keys[i] != s:
return False
return True
def main():
students = ["yo" , "a", "b", "c", 'd']
# student_names= [("Yada", 10.0, 2) , ("Nancy", 1.0, 5) , ("Wally", 2.3, 3.5), ("Carrot", 2.0, 2),("Bob", 1.0, 2), ("Hellium", 3, 10), ("Hala", 4, 1.0), ("Peet",5.0, 1.0), ("Actor1", 2.3, 4), ("Melanie",3.0, 5.0)]
# ###print(student_names)
gradebook = Gradebook(students, 2)
#so I need to do this basically.
if __name__ == "__main__":
import cProfile
cProfile.run("main()")