Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [SOLUTION] Secret Santas (#2)

Reply
Thread Tools

[SOLUTION] Secret Santas (#2)

 
 
Peter McMaster
Guest
Posts: n/a
 
      10-05-2004
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.len gth)))
puts person.status
end
end

__END__
Luke Skywalker <>
Leia Skywalker <>
Toula Portokalos <>
Gus Portokalos <>
Bruce Wayne <>
Virgil Brigman <>
Lindsey Brigman <>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OT: a secret. I have a secret. I have a secret. I won't tell you. Nanannannanana. Psychometrically Validated MCSE 13 02-22-2006 03:29 AM
[SUMMARY] Secret Santas (#2) Ruby Quiz Ruby 0 10-07-2004 01:00 PM
[SOLUTION] Secret Santas (#2) Peter McMaster Ruby 0 10-06-2004 12:04 AM
[QUIZ] Secret Santas (#2) Ruby Quiz Ruby 54 10-05-2004 07:54 PM
[SOLUTION] Secret Santas (#2) Ara.T.Howard Ruby 10 10-05-2004 01:28 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57