Skip to content

Instantly share code, notes, and snippets.

@mattfitzgerald
Last active December 7, 2019 05:26
Show Gist options
  • Save mattfitzgerald/552c9385d78197c4bae8754f9b657cea to your computer and use it in GitHub Desktop.
Save mattfitzgerald/552c9385d78197c4bae8754f9b657cea to your computer and use it in GitHub Desktop.
# will not pair people with a member of their houshold
households = [%w(Jeannie Terry\ Snr), ["Benton"], ["Terry"], %w(Mandy Marcos Elías Mateo), %w(Erin Matt)]
households.sort!{|a,b| b.length <=> a.length}
names = households.flatten
pairs = {}
names.each do |name|
household = households.find{|h| h.include?(name)}
available_names = (names - pairs.values - household)
if i == names.length - 2 and available_names.include?(names.last) #inelegant
# prevent an unmatchable last pair
pairs[name] = names.last
else
pairs[name] = available_names.sample
end
puts "#{name.ljust(9)} gives a book to #{pairs[name]}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment