Skip to content

Instantly share code, notes, and snippets.

@RobertAKARobin
Created March 31, 2016 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobertAKARobin/830375a60296cee17e128dd5daab6ca0 to your computer and use it in GitHub Desktop.
Save RobertAKARobin/830375a60296cee17e128dd5daab6ca0 to your computer and use it in GitHub Desktop.
Student matcher
names = [
"Alice",
"Bob",
"Carol",
"David",
"Eliza",
"Frank",
"Gretchen",
"Harry",
"Irene",
"Joe",
"Karen",
"Luke",
"Mary",
"Nick",
"Olga",
"Pete"
]
numChoices = 5
numPerGroup = 3
peeps = names.map do |name|
{
name: name,
choices: [],
chosenBy: [],
matched: false
}
end
peeps.each do |person|
choices = peeps.dup
choices.delete person
names = []
numChoices.times do
choice = choices.sample
next if person[:choices].include? choice
person[:choices].push choice
choice[:chosenBy].push person
end
puts "#{person[:name]} wants #{person[:choices].collect{|c| c[:name]}.join(",")}"
end
peeps.sort!{|a,b| b[:chosenBy].size <=> a[:chosenBy].size }
puts "*" * 10
peeps.each do |person|
puts "#{person[:name]} chosen by #{person[:chosenBy].collect{|c| c[:name]}.join(",")}"
end
puts "*" * 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment