[SOLUTION] Secret Santas (#2)

Discussion in 'Ruby' started by Peter McMaster, Oct 6, 2004.

  1. Hello!

    I realised that my implementation is rather flawed, and rather late, so
    decided to not bother with the less exciting SMTP bit. Also, while
    testing (which failed :) I was using DATA instead of STDIN. I just left
    it like this; I find very useful, and am keen to spread the love. (This
    means that the end of the file is under the "Lindsey Brigman" line, not
    where it says __END__)

    Occasionally it freaks out and is unable to assign valid santas to
    either the last or second-last persons, as they've already been
    assigned elsewhere. A cleverer friend helpfully suggested "looks like
    you need some cleverer algorithm"...

    I think that if implementing this again I'd just build two identical
    lists of people, and keep swapping them around randomly until it was
    all good. Less efficient, but more likely to eventually come up with
    the goods reliably.

    Had lots of fun doing this!

    - Peter

    #!/usr/bin/env ruby

    class Person
    attr_reader :sname, :email
    attr_accessor :santa
    def initialize(details)
    @fname, @sname, @email = details.scan(/(\w+)\s+(\w+)\s+<(.*)>/)[0]
    end
    def to_s() @fname + " " + @sname end
    def status()
    if @santa then self.to_s + " is santa'd to " + @santa.to_s
    else self.to_s + " is santa-less!\a" end # Ooh... beeps.
    end
    end

    families = Hash.new {[]}
    unchosen = []

    DATA.each do |l|
    someone = Person.new(l)
    families[someone.sname] <<= someone
    unchosen << someone
    end

    families.keys.each do |sname|
    choices = unchosen.dup.delete_if { |someone| someone.sname == sname }
    families[sname].each do |person|
    person.santa =
    unchosen.delete(choices.delete_at(rand(choices.length)))
    puts person.status
    end
    end

    __END__
    Luke Skywalker <>
    Leia Skywalker <>
    Toula Portokalos <>
    Gus Portokalos <>
    Bruce Wayne <>
    Virgil Brigman <>
    Lindsey Brigman <>
     
    Peter McMaster, Oct 6, 2004
    #1
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Y2FzaGRlc2ttYWM=?=

    Solution file not in the solution folder

    =?Utf-8?B?Y2FzaGRlc2ttYWM=?=, Sep 12, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    1,300
    Laurent Bugnion
    Sep 12, 2006
  2. , India
    Replies:
    17
    Views:
    1,392
    James Kanze
    Oct 1, 2007
  3. Replies:
    8
    Views:
    764
  4. Ruby Quiz

    [QUIZ] Secret Santas (#2)

    Ruby Quiz, Oct 1, 2004, in forum: Ruby
    Replies:
    54
    Views:
    691
    Thomas Leitner
    Oct 5, 2004
  5. Ara.T.Howard

    [SOLUTION] Secret Santas (#2)

    Ara.T.Howard, Oct 4, 2004, in forum: Ruby
    Replies:
    10
    Views:
    332
  6. Peter McMaster

    [SOLUTION] Secret Santas (#2)

    Peter McMaster, Oct 6, 2004, in forum: Ruby
    Replies:
    0
    Views:
    209
    Peter McMaster
    Oct 6, 2004
  7. Ruby Quiz

    [SUMMARY] Secret Santas (#2)

    Ruby Quiz, Oct 7, 2004, in forum: Ruby
    Replies:
    0
    Views:
    229
    Ruby Quiz
    Oct 7, 2004
  8. email55555 email55555

    [SOLUTION] Ruby Quiz #14 LCD Numbers ( solution #2 )

    email55555 email55555, Jan 9, 2005, in forum: Ruby
    Replies:
    16
    Views:
    453
    David Tran
    Jan 10, 2005
Loading...