[SOLUTION] Secret Santas (#2)

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

  1. 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.]"
     
    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:
    973
  2. Ruby Quiz

    [QUIZ] Secret Santas (#2)

    Ruby Quiz, Oct 1, 2004, in forum: Ruby
    Replies:
    54
    Views:
    506
    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:
    228
  4. Peter McMaster

    [SOLUTION] Secret Santas (#2)

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

    [SUMMARY] Secret Santas (#2)

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

Share This Page