ridleygarnier

Untitled

Feb 19th, 2020
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | None | 0 0
  1. class WindowGrouper(Grouper):
  2.     """
  3.    A grouper used to create a grouping of students according to their
  4.    answers to a survey. This grouper uses a window search 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.  
  17.     def make_grouping(self, course: Course, survey: Survey) -> Grouping:
  18.         """
  19.        Return a grouping for all students in <course>.
  20.  
  21.        Starting with a tuple of all students in <course> obtained by calling
  22.        the <course>.get_students() method, create groups of students using the
  23.        following algorithm:
  24.  
  25.        1. Get the windows of the list of students who have not already been
  26.           put in a group.
  27.        2. For each window in order, calculate the current window's score as
  28.           well as the score of the next window in the list. If the current
  29.           window's score is greater than or equal to the next window's score,
  30.           make a group out of the students in current window and start again at
  31.           step 1. If the current window is the last window, compare it to the
  32.           first window instead.
  33.  
  34.        In step 2 above, use the <survey>.score_students to determine the score
  35.        of each window (list of students).
  36.  
  37.        In step 1 and 2 above, use the windows function to get the windows of
  38.        the list of students.
  39.  
  40.        If there are any remaining students who have not been put in a group
  41.        after repeating steps 1 and 2 above, put the remaining students into a
  42.        new group.
  43.        """
  44.         students = list(course.get_students())
  45.         s = students.copy()
  46.         master = Grouping()
  47.  
  48.         while not not s:
  49.             window = windows(s, self.group_size)
  50.             for i in range(len(window)):
  51.                 if i + 1 != len(window):
  52.                     if survey.score_students(window[i]) > \
  53.                             survey.score_students(window[i+1]):
  54.                         x = []
  55.                         for lst in window[i:i+2]:
  56.                             for student in lst:
  57.                                 x.append(student)
  58.                                 s.remove(student)
  59.                         master.add_group(Group(x))
  60.  
  61.                 else:
  62.                     if survey.score_students(window[i]) > \
  63.                             survey.score_students(window[0]):
  64.                         x = []
  65.                         for lst in window[i] + window[0]:
  66.                             for student in lst:
  67.                                 x.append(student)
  68.                                 s.remove(student)
  69.                         master.add_group(Group(x))
  70.         return master
Add Comment
Please, Sign In to add comment