-
-
Save lseelenbinder/505747 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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 = [] | |
users << User.new(has=["A", "C"], wants=["B", "D"]) | |
users << User.new(has=["D"], wants=["A"]) | |
users << User.new(has=["A", "B", "D"], wants=["C"]) | |
def find_best(users, user) | |
best = users.first | |
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 | |
users.length.times do |i| | |
user = users[i] | |
u = find_best(users, user) | |
puts "The best match for user #{ user.wants } is #{ u.has }" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment