I am trying to write a python script to divide the students registered into classes. Let's say I have 81 students and the max capacity is 20 students I want to get 5 classes with 16-17 student each, not 5 classes with 20 20 20 20 and 1 student alone.
Thanks a lot!
Edit : i solved it. here is the algorithm if anyone is interested
import math
def partition_students(students, max_capacity):
students.sort(key=lambda student: student.last_name)
num_students = len(students)
num_classes = math.ceil(num_students / max_capacity)
num_students_per_class = math.ceil(num_students / num_classes)
classes = [[] for _ in range(num_classes)] # Initialize classes with empty lists
class_index = -1
for i, student in enumerate(students):
if i % num_students_per_class == 0:
class_index = (class_index + 1) % num_classes # Update class_index
classes[class_index].append(student)
return classes