Ruby implementation of Secret Santa -
the problem stated follows: there list of persons, each having first name , last name. people having same last name belong same family. aim chose secret santa each person such secret santa ofcourse not same person, nor belong same family. i've taken liberty of assigning santa same family if there no other choice.
i have following code achieve , passing tests - i'm not confident if code produces correct results in cases.
in case algorithm not correct i'll appreciate if can explain why algorithm fails, review of code quite helpful.
the main method in code assign_angels, , have tried return self of methods following east oriented code
this problem taken ruby quiz: secret santa
i'm using equalizer module equality checking equalizer
require 'set' require 'equalizer' class people include equalizer.new(:people) def initialize personlist = [] @people = array(personlist).to_set end def add person @people.add person self end def assign_angels @people.each |person| eligible_santas .preferably_of_different_family(person) .assign_angel_to_one(person) end self end def to_s @people.map { |person| person.to_s } end protected def sample @people.to_a.sample || nilperson.new end def size @people.size end def assign_angel_to_one person sample.assign_angel person self end def eligible_santas people.new @people.select { |person| person.angel == nil } end def preferably_of_different_family person_a different_family = people_of_different_family person_a if different_family.size == 0 same_family = people_of_same_family person_a if(same_family.size == 0) return people.new nilperson.new end return same_family end return different_family end def people_of_different_family person_a people.new @people.select { |person| person unless person.is_family? person_a } end def people_of_same_family person_a people.new @people.select { |person| person unless person == person_a } end attr_reader :people end
Comments
Post a Comment