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-06-2004
Less fun, but more reliable possibly, is the
mess-them-around-randomly-until-it-looks-right option.

It does this very stupidly, and will run indefinitely if you feed it
something for which there is no solution. (If you're really unlucky it
will run indefinitely if you feed it something really simple!)

- Peter

#!/usr/bin/env ruby

class Person
attr_reader :sname, :email
def initialize(details)
@fname, @sname, @email = details.scan(/(\w+)\s+(\w+)\s+<(.*)>/)[0]
end
def to_s() @fname + " " + @sname end
end

a, b = [], []

STDIN.each do |l|
someone = Person.new(l)
a << someone; b << someone
end

puts "Mixing..."

passes = 0
begin
ok = true
a.each_index do |idx|
passes += 1
if a[idx].sname == b[idx].sname
ok = false
r = rand(b.length); b[idx], b[r] = b[r], b[idx]
end
end
end until ok

a.each_index { |idx| puts "#{a[idx]} is santa'd with #{b[idx]}" }

puts "[#{passes} passes required.]"



 
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-05-2004 11:17 PM
[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