[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. Advertising

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. et

    What's the secret?

    et, Nov 13, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    967
  2. Ruby Quiz

    [QUIZ] Secret Santas (#2)

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

    [SOLUTION] Secret Santas (#2)

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

    [SOLUTION] Secret Santas (#2)

    Peter McMaster, Oct 6, 2004, in forum: Ruby
    Replies:
    0
    Views:
    126
    Peter McMaster
    Oct 6, 2004
  5. Ruby Quiz

    [SUMMARY] Secret Santas (#2)

    Ruby Quiz, Oct 7, 2004, in forum: Ruby
    Replies:
    0
    Views:
    136
    Ruby Quiz
    Oct 7, 2004
Loading...

Share This Page