def test_sort_students() -> None: s1 = course.Student(1, "gad") s2 = course.Student(53, "floof") s3 = course.Student(9, "gad") s4 = course.Student(15, "blep") s5 = course.Student(4, "meep") ppl = [s1, s2, s3, s4, s5] assert course.sort_students(ppl, 'id') == [s1, s5, s3, s4, s2] assert course.sort_students(ppl, 'name') == [s4, s2, s1, s3, s5]
def make_grouping(self, course: Course, survey: Survey) -> Grouping: """ Return a grouping for all students in <course>. The first group should contain the students in <course> whose names come first when sorted alphabetically, the second group should contain the next students in that order, etc. All groups in this grouping should have exactly self.group_size members except for the last group which may have fewer than self.group_size members if that is required to make sure all students in <course> are members of a group. Hint: the sort_students function might be useful """ groupings = Grouping() list_of_groups = [] new_list = sort_students(course.students, 'name') split_list = slice_list(new_list, self.group_size) for item in split_list: d = Group(item) list_of_groups.append(d) for items in list_of_groups: groupings.add_group(items) return groupings
def test_get_student(self, empty_course, students_out_of_order, students): students1 = course.sort_students(students_out_of_order, "id") empty_course.enroll_students(students1) funct = empty_course.get_students() test = tuple(students) for i in range(len(funct)): assert funct[i].id == test[i].id
def make_grouping(self, course: Course, survey: Survey) -> Grouping: """ Return a grouping for all students in <course>. The first group should contain the students in <course> whose names come first when sorted alphabetically, the second group should contain the next students in that order, etc. All groups in this grouping should have exactly self.group_size members except for the last group which may have fewer than self.group_size members if that is required to make sure all students in <course> are members of a group. Hint: the sort_students function might be useful """ students_in_course_copy = course.students[:] students_sorted_alpha = sort_students(students_in_course_copy, "name") students_grouped = slice_list(students_sorted_alpha, self.group_size) students_group_objs = [] grouping = Grouping() for group in students_grouped: g = Group(group) students_group_objs.append(g) for group in students_group_objs: grouping.add_group(group) return grouping
def make_grouping(self, course: Course, survey: Survey) -> Grouping: """ Return a grouping for all students in <course>. The first group should contain the students in <course> whose names come first when sorted alphabetically, the second group should contain the next students in that order, etc. All groups in this grouping should have exactly self.group_size members except for the last group which may have fewer than self.group_size members if that is required to make sure all students in <course> are members of a group. Hint: the sort_students function might be useful """ # TODO: complete the body of this method #sort the student by name sorted_stu = sort_students(course.get_students(), 'name') sliced_list = slice_list(sorted_stu, self.group_size) g = Grouping() for batch in sliced_list: temp_group = Group(batch) g.add_group(temp_group) return g
def make_grouping(self, course: Course, survey: Survey) -> Grouping: """ Return a grouping for all students in <course>. The first group should contain the students in <course> whose names come first when sorted alphabetically, the second group should contain the next students in that order, etc. All groups in this grouping should have exactly self.group_size members except for the last group which may have fewer than self.group_size members if that is required to make sure all students in <course> are members of a group. Hint: the sort_students function might be useful """ lst_students = course.get_students() new_lst = sort_students(list(lst_students), 'name') grouping = Grouping() if self.group_size < len(new_lst): new_lst = slice_list(new_lst, self.group_size) for sublist in new_lst: group = Group(sublist) grouping.add_group(group) else: group = Group(new_lst) grouping.add_group(group) return grouping
def make_grouping(self, course: Course, survey: Survey) -> Grouping: """ Return a grouping for all students in <course>. The first group should contain the students in <course> whose names come first when sorted alphabetically, the second group should contain the next students in that order, etc. All groups in this grouping should have exactly self.group_size members except for the last group which may have fewer than self.group_size members if that is required to make sure all students in <course> are members of a group. Hint: the sort_students function might be useful """ lst = [] # list of students to_return = Grouping() for stud in course.get_students(): # shallow list of course.student lst.append(stud) # the student in the course if len(lst) == 0: return to_return size = self.group_size lst = sort_students(lst, "name") # sorting the students sliced_list = slice_list(lst, size) # divided the students properly for sublist in sliced_list: # need to make them all groups to_return.add_group(Group(sublist)) # each sublist is a group return to_return
def _get_max_student(lst1: List[Student], lst2: List[Student], survey: Survey) -> Optional[Student]: """ Return the student in lst1 such that when moved him to lst2, the score of survey will be the highest compare to other students in lst1 Precondition: len(lst1) > 0 and len(lst2) > 0 """ if len(lst1) == 0: return None scores = {} for student in lst1: lst2.append(student) new_score = survey.score_students(lst2) if new_score not in scores.keys(): scores[new_score] = [student] else: scores[new_score].append(student) lst2.remove(student) max_students = scores[max(tuple(scores.keys()))] sort_students(max_students, 'id') return max_students[0]
def make_grouping(self, course: Course, survey: Survey) -> Grouping: """ Return a grouping for all students in <course>. Starting with a tuple of all students in <course> obtained by calling the <course>.get_students() method, create groups of students using the following algorithm: 1. select the first student in the tuple that hasn't already been put into a group and put this student in a new group. 2. select the student in the tuple that hasn't already been put into a group that, if added to the new group, would increase the group's score the most (or reduce it the least), add that student to the new group. 3. repeat step 2 until there are N students in the new group where N is equal to self.group_size. 4. repeat steps 1-3 until all students have been placed in a group. In step 2 above, use the <survey>.score_students method to determine the score of each group of students. The final group created may have fewer than N members if that is required to make sure all students in <course> are members of a group. """ temp_list = list(course.get_students()) student_tuple = sort_students(temp_list, 'id') student_list = list(student_tuple) grouping = Grouping() for student in student_tuple: # step 1 if student in student_list: temp_group = [student] student_list.remove(student) while self.group_size > len(temp_group) and \ len(student_list) > 0: max_score = 0.0 max_student = student_list[0] for student1 in student_list: temp_score = survey.score_students \ (temp_group + [student1]) if temp_score > max_score: max_score = temp_score max_student = student1 temp_group.append(max_student) student_list.remove(max_student) grouping.add_group(Group(temp_group)) return grouping
def test_course() -> None: course_0 = Course("Snake") course_0.enroll_students([Student(0, "Tim")]) course_0.enroll_students([Student(0, "Tim")]) course_0.enroll_students([Student(0, "Tim"), Student(0, "Roney")]) assert len(course_0.get_students()) == 1 course_0.enroll_students([Student(1, "Tim"), Student(2, "Roney")]) assert len(course_0.get_students()) == 3 course_1 = Course("Python") students = [] for x in range(10): students.append(Student(x, ''.join( random.choices(string.ascii_uppercase + string.digits, k=6)))) course_1.enroll_students(students) assert len(course_1.get_students()) == len(students) assert tuple(sort_students(students, "id")) == course_1.get_students()
def make_grouping(self, course: Course, survey: Survey) -> Grouping: """ Return a grouping for all students in <course>. Starting with a tuple of all students in <course> obtained by calling the <course>.get_students() method, create groups of students using the following algorithm: 1. Get the windows of the list of students who have not already been put in a group. 2. For each window in order, calculate the current window's score as well as the score of the next window in the list. If the current window's score is greater than or equal to the next window's score, make a group out of the students in current window and start again at step 1. If the current window is the last window, compare it to the first window instead. In step 2 above, use the <survey>.score_students to determine the score of each window (list of students). In step 1 and 2 above, use the windows function to get the windows of the list of students. If there are any remaining students who have not been put in a group after repeating steps 1 and 2 above, put the remaining students into a new group. """ students_list = sort_students(list(course.get_students()), 'id') return_grouping = Grouping() # While we still have windows, loop while len(students_list) > 0: # Find local max window local_max_window = self.find_local_max_window( students_list, survey) # add window as new group into grouping return_grouping.add_group(Group(local_max_window)) # remove window's elements for st in local_max_window: students_list.remove(st) return return_grouping
def make_grouping(self, course: Course, survey: Survey) -> Grouping: """ Return a grouping for all students in <course>. The first group should contain the students in <course> whose names come first when sorted alphabetically, the second group should contain the next students in that order, etc. All groups in this grouping should have exactly self.group_size members except for the last group which may have fewer than self.group_size members if that is required to make sure all students in <course> are members of a group. Hint: the sort_students function might be useful """ # TODO: complete the body of this method roster = course.get_students() roster_alpha = sort_students(roster, 'name') grouped = slice_list(roster_alpha, self.group_size) return grouped
def make_grouping(self, course: Course, survey: Survey) -> Grouping: """ Return a grouping for all students in <course>. Starting with a tuple of all students in <course> obtained by calling the <course>.get_students() method, create groups of students using the following algorithm: 1. select the first student in the tuple that hasn't already been put into a group and put this student in a new group. 2. select the student in the tuple that hasn't already been put into a group that, if added to the new group, would increase the group's score the most (or reduce it the least), add that student to the new group. 3. repeat step 2 until there are N students in the new group where N is equal to self.group_size. 4. repeat steps 1-3 until all students have been placed in a group. In step 2 above, use the <survey>.score_students method to determine the score of each group of students. The final group created may have fewer than N members if that is required to make sure all students in <course> are members of a group. """ tup = course.get_students() g = Grouping() lst = list(tup) lst = sort_students(lst, 'id') while True: grouplst = [] if len(lst) <= self.group_size: group = Group(lst) g.add_group(group) return g grouplst.append(lst[0]) lst.pop(0) for i in range(1, self.group_size): grouplst.append(bigval(grouplst, lst, survey)) lst.remove(bigval(grouplst, lst, survey)) group = Group(grouplst) g.add_group(group)
def make_grouping(self, course: Course, survey: Survey) -> Grouping: """ Return a grouping for all students in <course>. The first group should contain the students in <course> whose names come first when sorted alphabetically, the second group should contain the next students in that order, etc. All groups in this grouping should have exactly self.group_size members except for the last group which may have fewer than self.group_size members if that is required to make sure all students in <course> are members of a group. Hint: the sort_students function might be useful """ sorted_students = sort_students(course.students, 'name') grouped_students = slice_list(sorted_students, self.group_size) grouping = Grouping() for group in grouped_students: grouping.add_group(Group(group)) return grouping
def test_sort_stud(students, students_out_of_order): new = course.sort_students(students_out_of_order, "id") for i in range(len(new)): assert new[i].id == students[i].id
def make_grouping(self, course: Course, survey: Survey) -> Grouping: """ Return a grouping for all students in <course>. Starting with a tuple of all students in <course> obtained by calling the <course>.get_students() method, create groups of students using the following algorithm: 1. select the first student in the tuple that hasn't already been put into a group and put this student in a new group. 2. select the student in the tuple that hasn't already been put into a group that, if added to the new group, would increase the group's score the most (or reduce it the least), add that student to the new group. # which would make the group the best group 3. repeat step 2 until there are N students in the new group where N is equal to self.group_size. 4. repeat steps 1-3 until all students have been placed in a group. In step 2 above, use the <survey>.score_students method to determine the score of each group of students. The final group created may have fewer than N members if that is required to make sure all students in <course> are members of a group. """ to_return = Grouping() lst_of_studs = [] for stud in course.get_students(): lst_of_studs.append(stud) if len(lst_of_studs) == 0: return to_return lst_of_studs = sort_students(lst_of_studs, "id") i = 0 while i < (len(course.get_students()) - 1): group_lst = [] while len(group_lst) != self.group_size and len(lst_of_studs) != 0: # important stopping conditions student1 = lst_of_studs[1] tups_of_scores_and_studs = [] if len(group_lst) == 0: group_lst.append(lst_of_studs[i]) lst_of_studs.pop(i) # first student to add for stud in lst_of_studs: tups_of_scores_and_studs.append( (survey.score_students(group_lst + [stud]), stud)) # appedning the sore of the group with the student lst_of_scores = [] for tup in tups_of_scores_and_studs: lst_of_scores.append(tup[0]) ind_of_highest = lst_of_scores.index(max(lst_of_scores)) # find the index of the tup with this score lst_of_studs.pop( lst_of_studs.index( tups_of_scores_and_studs[ind_of_highest][1])) # removing the student with the highest score from lst of studs group_lst.append(tups_of_scores_and_studs[ind_of_highest][1]) # adding that student to group_lst if student1 not in lst_of_studs: # for starting from beggining i += 1 else: i = 0 if len(group_lst) != 0: group = Group(group_lst) to_return.add_group(group) if len(lst_of_studs) == 0: i += 1 if len(lst_of_studs) != 0: lst = [] for stud in lst_of_studs: lst.append(stud) to_return.add_group(Group(lst)) # for dealing with the remainder return to_return
def make_grouping(self, course: Course, survey: Survey) -> Grouping: """ Return a grouping for all students in <course>. Starting with a tuple of all students in <course> obtained by calling the <course>.get_students() method, create groups of students using the following algorithm: 1. Get the windows of the list of students who have not already been put in a group. 2. For each window in order, calculate the current window's score as well as the score of the next window in the list. If the current window's score is greater than or equal to the next window's score, make a group out of the students in current window and start again at step 1. If the current window is the last window, compare it to the first window instead. In step 2 above, use the <survey>.score_students to determine the score of each window (list of students). In step 1 and 2 above, use the windows function to get the windows of the list of students. If there are any remaining students who have not been put in a group after repeating steps 1 and 2 above, put the remaining students into a new group. """ n = self.group_size tup_of_studs = course.get_students() lst_of_studs = [] to_return = Grouping() for stud in tup_of_studs: lst_of_studs.append(stud) if len(lst_of_studs) == 0: return to_return lst_of_studs = sort_students(lst_of_studs, "id") lst_of_windows = windows(lst_of_studs, n) shallow_lst_of_windows = [] for window in lst_of_windows: shallow_lst_of_windows.append(window) i = 0 while i < len(shallow_lst_of_windows) - 1: if survey.score_students(shallow_lst_of_windows[i]) >= \ survey.score_students(shallow_lst_of_windows[i + 1]): to_return.add_group(Group(shallow_lst_of_windows[i])) # add group to grouping for stud in shallow_lst_of_windows[i]: # this avoids repeats lst_of_studs.remove(stud) shallow_lst_of_windows = windows(lst_of_studs, n) i = 0 else: # have to start comparing from the beggining again i += 1 if len(shallow_lst_of_windows) != 0: # comparing the last with the first if survey.score_students(shallow_lst_of_windows[-1]) >= \ survey.score_students(shallow_lst_of_windows[0]): to_return.add_group(Group(shallow_lst_of_windows[-1])) shallow_lst_of_windows.pop(-1) if len(shallow_lst_of_windows) != 0: # if some did not get matched for lst in shallow_lst_of_windows: to_return.add_group(Group(lst)) # want to increment return to_return