Is there any algorithm to form groups based on ranking?

Viewed 190

I'm looking for an algorithm to form groups based on ranking choices. For example, I have a class of 20 and I would want groups of 3 and the rest to be in group of 2, and the students will rank perhaps top 6 people they would like to group with.

I've looked into the stable marriage algorithm but I feel it's not very suitable as it's better for pairs.

1 Answers

First let's sort the data. You can decide how to compare pairs (tuple in some programming languages).

students = [("A", 3.1),...]
students.sort(key=lambda s: -s[1])  # your ranking function will be here

If you want top 6 will be in group 1, you can extract from array.

students[0:6]

You can split students into multiple groups if you know number of students in a group.

number_of_students_in_group = [6, 6, 7]
first_idx = 0
for group_idx, group_size in enumerate(number_of_students_in_group):
    print("group", group_idx, ":", students[first_idx:first_idx+group_size])
    first_idx += group_size
Related