Last active
August 29, 2015 14:13
-
-
Save ltw/a7bef1ffb17794300fcd to your computer and use it in GitHub Desktop.
GroupMaker Pro™ (all props to @gfredericks)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'set' | |
| names = [ | |
| # student names here | |
| ] | |
| def get_group_set(coll) | |
| if coll.count % 4 == 0 | |
| coll.each_slice(4).to_a | |
| else | |
| [coll[0..2]] + get_group_set(coll[3..-1]) | |
| end | |
| end | |
| def get_random_group_set(students) | |
| get_group_set(students.shuffle).map(&:to_set) | |
| end | |
| def compatible_group_sets?(p1, p2) | |
| (p1 & p2).count <= 2 | |
| end | |
| def find_groups(students, max_tries, max_groups=10) | |
| return [] if students.count < 6 | |
| group_sets = [get_random_group_set(students)] | |
| tries = 0 | |
| while tries < max_tries | |
| break if group_sets.count > max_groups | |
| new_group_set = get_random_group_set(students) | |
| if group_sets.all? {|p| compatible_group_sets?(p, new_group_set) } | |
| group_sets << new_group_set | |
| tries = 0 | |
| else | |
| tries += 1 | |
| end | |
| end | |
| group_sets | |
| end | |
| groups = find_groups(names, 2, 5) | |
| groups.map do |x| | |
| puts x.each_with_index.map {|y,i| "#{i+1}: #{y.to_a.join(', ')}" } | |
| puts | |
| end | |
| puts "There were no groups found for these students." if groups.empty? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment