Skip to content

Instantly share code, notes, and snippets.

@lseelenbinder
Created August 3, 2010 02:32
Show Gist options
  • Save lseelenbinder/505729 to your computer and use it in GitHub Desktop.
Save lseelenbinder/505729 to your computer and use it in GitHub Desktop.
#! /usr/bin/ruby
# This program takes a list of users, their desires, and their possessions.
# Then it asks for a particular user and matches them with the best user
# for their desires.
class User
attr_reader :has, :wants
attr_accessor :score
def initialize (has=[], wants=[])
@score = 0
@has = has
@wants = wants
end
end
users = []
user1 = User.new(has=["A", "C"], wants=["B", "D"])
users << user1
user2 = User.new(has=["D"], wants=["A"])
users << user2
user3 = User.new(has=["A", "B", "D"], wants="C")
users << user3
def find_best(users=users, user=user1)
best = user
users.each do |u|
user.wants.each { |w| u.score += 1 if u.has.include? w }
best = u if u.score > best.score
break if u.score == user.wants.length
end
best
end
p find_best(users = users, user = user1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment