Created
August 3, 2010 01:20
-
-
Save lseelenbinder/505661 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
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