Advertisement
ridleygarnier

Untitled

Feb 19th, 2020
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.37 KB | None | 0 0
  1. class GreedyGrouper(Grouper):
  2.     """
  3.    A grouper used to create a grouping of students according to their
  4.    answers to a survey. This grouper uses a greedy algorithm to create
  5.    groups.
  6.  
  7.    === Public Attributes ===
  8.    group_size: the ideal number of students that should be in each group
  9.  
  10.    === Representation Invariants ===
  11.    group_size > 1
  12.    """
  13.  
  14.     group_size: int
  15.  
  16.     def make_grouping(self, course: Course, survey: Survey) -> Grouping:
  17.         """
  18.        Return a grouping for all students in <course>.
  19.  
  20.        Starting with a tuple of all students in <course> obtained by calling
  21.        the <course>.get_students() method, create groups of students using the
  22.        following algorithm:
  23.  
  24.        1. select the first student in the tuple that hasn't already been put
  25.           into a group and put this student in a new group.
  26.        2. select the student in the tuple that hasn't already been put into a
  27.           group that, if added to the new group, would increase the group's
  28.           score the most (or reduce it the least), add that student to the new
  29.           group.
  30.        3. repeat step 2 until there are N students in the new group where N is
  31.           equal to self.group_size.
  32.        4. repeat steps 1-3 until all students have been placed in a group.
  33.  
  34.        In step 2 above, use the <survey>.score_students method to determine
  35.        the score of each group of students.
  36.  
  37.        The final group created may have fewer than N members if that is
  38.        required to make sure all students in <course> are members of a group.
  39.        """
  40.         student_tuples = course.get_students()
  41.         students = (list(student_tuples))
  42.         master = Grouping()
  43.         x = students[1]
  44.  
  45.         while not students != []:
  46.             group_list = []
  47.             if len(master) == 0:
  48.                 group_list.append(student_tuples[0])
  49.             else:
  50.                 while len(group_list) <= self.group_size and not \
  51.                         len(students) == 0:
  52.                     for student in students:
  53.                         if survey.score_students([student]) > \
  54.                                 survey.score_students([x]):
  55.                             x = student
  56.                         elif survey.score_students([student]) == \
  57.                                 survey.score_students([x]):
  58.                             if student.id > x.id:
  59.                                 x = student
  60.                     students.remove(x)
  61.                     group_list.append(x)
  62.                 master.add_group(Group(group_list))
  63.         return master
  64.  
  65.  
  66. class WindowGrouper(Grouper):
  67.     """
  68.    A grouper used to create a grouping of students according to their
  69.    answers to a survey. This grouper uses a window search algorithm to create
  70.    groups.
  71.  
  72.    === Public Attributes ===
  73.    group_size: the ideal number of students that should be in each group
  74.  
  75.    === Representation Invariants ===
  76.    group_size > 1
  77.    """
  78.  
  79.     group_size: int
  80.  
  81.  
  82.     def make_grouping(self, course: Course, survey: Survey) -> Grouping:
  83.         """
  84.        Return a grouping for all students in <course>.
  85.  
  86.        Starting with a tuple of all students in <course> obtained by calling
  87.        the <course>.get_students() method, create groups of students using the
  88.        following algorithm:
  89.  
  90.        1. Get the windows of the list of students who have not already been
  91.           put in a group.
  92.        2. For each window in order, calculate the current window's score as
  93.           well as the score of the next window in the list. If the current
  94.           window's score is greater than or equal to the next window's score,
  95.           make a group out of the students in current window and start again at
  96.           step 1. If the current window is the last window, compare it to the
  97.           first window instead.
  98.  
  99.        In step 2 above, use the <survey>.score_students to determine the score
  100.        of each window (list of students).
  101.  
  102.        In step 1 and 2 above, use the windows function to get the windows of
  103.        the list of students.
  104.  
  105.        If there are any remaining students who have not been put in a group
  106.        after repeating steps 1 and 2 above, put the remaining students into a
  107.        new group.
  108.        """
  109.         students = list(course.get_students())
  110.         s = students.copy()
  111.         master = Grouping()
  112.  
  113.         while s:
  114.             window = windows(s, self.group_size)
  115.             for i in range(len(window)):
  116.                 if i + 1 != len(window):
  117.                     if survey.score_students(window[i]) > \
  118.                             survey.score_students(window[i+1]):
  119.                         x = []
  120.                         for lst in window[i:i+2]:
  121.                             for student in lst:
  122.                                 x.append(student)
  123.                                 s.remove(student)
  124.                         master.add_group(Group(x))
  125.  
  126.                 else:
  127.                     if survey.score_students(window[i]) > \
  128.                             survey.score_students(window[0]):
  129.                         x = []
  130.                         lst = window[i] + window[0]
  131.                         for student in lst:
  132.                             x.append(student)
  133.                             s.remove(student)
  134.                         master.add_group(Group(x))
  135.         return master
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement