[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. 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,298
    Laurent Bugnion
    Sep 12, 2006
  2. , India
    Replies:
    17
    Views:
    1,391
    James Kanze
    Oct 1, 2007
  3. Replies:
    8
    Views:
    760
  4. Ruby Quiz

    [QUIZ] Secret Santas (#2)

    Ruby Quiz, Oct 1, 2004, in forum: Ruby
    Replies:
    54
    Views:
    690
    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:
    331
  6. Peter McMaster

    [SOLUTION] Secret Santas (#2)

    Peter McMaster, Oct 6, 2004, in forum: Ruby
    Replies:
    0
    Views:
    152
    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:
    452
    David Tran
    Jan 10, 2005
Loading...