Match an element of an array to a different element in that array
Posted
by
Anh
on Stack Overflow
See other posts from Stack Overflow
or by Anh
Published on 2014-06-07T03:20:28Z
Indexed on
2014/06/07
3:24 UTC
Read the original article
Hit count: 215
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
© Stack Overflow or respective owner