Created
October 2, 2013 01:58
-
-
Save ivanbrennan/6788042 to your computer and use it in GitHub Desktop.
Blog post scheduler
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
| # create a method called create_groups that, given an array of student names, | |
| # a group size, and a number of groups, will return an array of groups of | |
| # of students with no student in adjacent groups | |
| def create_groups(students, group_size, num_of_groups) | |
| out_of_order = students.shuffle | |
| groups = [] | |
| index = 0 | |
| num_of_groups.times do | |
| group = [] | |
| group_size.times do | |
| group << out_of_order[index] | |
| index = (index+=1) % students.count | |
| end | |
| groups << group | |
| end | |
| groups | |
| end | |
| # Another approach | |
| def create_groups_alt(students, group_size, num_of_groups) | |
| # (total size needed) / (current size) | |
| grow_by = ((num_of_groups * group_size).to_f / students.count.to_f).ceil | |
| out_of_order = students.shuffle * grow_by | |
| groups = [] | |
| out_of_order.each_slice(group_size) do |group| | |
| groups << group if groups.count < num_of_groups | |
| end | |
| groups | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment