Skip to content

Instantly share code, notes, and snippets.

@loganhasson
Created October 2, 2013 03:04
Show Gist options
  • Save loganhasson/6788576 to your computer and use it in GitHub Desktop.
Save loganhasson/6788576 to your computer and use it in GitHub Desktop.
# 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(student_list, group_size, num_groups)
groups_array = []
random_list = student_list.shuffle
while random_list.size > 0
mini_group = random_list.slice(0,group_size)
random_list = random_list.drop(group_size)
groups_array << mini_group
end
groups_array.collect do |group|
i = 0
while group.size < group_size
group << groups_array[i]
i += 1
end
end
i = 0
while groups_array.size < num_groups
extra_group = groups_array[i]
groups_array << extra_group
if i > groups_array.size - 1
i = 0
else
i += 1
end
end
groups_array
end
# take the list and divide it up into a list of group size
# if there aren't enough students to make the correct number of groups,
# take students from beginning of list and keep adding
@dannyc5
Copy link

dannyc5 commented Oct 2, 2013

group << groups_array[i]

Doesn't this push in a mini-group as only 1 element into the group?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment