Match an element of an array to a different element in that array
- by Anh
I have an array containing several students. I want them to cross-grade one another randomly, i.e. each student will grade someone and will be graded by someone else (these two people may or may not be the same person).
Here is my working solution. I'm sure there is a more elegant answer!
def randomize(student_array)
graders = student_array.dup
gradees = student_array.dup
result = {}
graders.each do |grader|
gradee = grader
while gradee == grader
gradee = gradees.sample
end
result[grader] = gradee
gradees.delete_at(gradees.index(gradee))
end
return result
end