Skip to content

Instantly share code, notes, and snippets.

@lseelenbinder
Created August 3, 2010 01:20
Show Gist options
  • Save lseelenbinder/505661 to your computer and use it in GitHub Desktop.
Save lseelenbinder/505661 to your computer and use it in GitHub Desktop.
class User():
def __init__(self, has=[], wants=[]):
self.has = has
self.wants = wants
self.score = 0
def find_best_match(users=[], user=None):
best = users[0]
for u in users:
for w in user.wants:
if w in u.has:
u.score += 1
#This user has all the wanted items
if u.score == len(user.wants):
return u
elif u.score > best.score:
best = u
else:
pass
return best
users = []
user1 = User(has=["A", "C"], wants=["B", "D"])
users.append(user1)
user2 = User(has=["D"], wants=["A"])
users.append(user2)
user3 = User(has=["A", "B", "D"], wants="C")
users.append(user3)
best_match = find_best_match(users=users, user=user1)
print best_match.score, best_match.has
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment