Created
July 28, 2021 00:42
-
-
Save l3gacyb3ta/bf196e504d47a700be07c7fdcb22a8a9 to your computer and use it in GitHub Desktop.
People matching for hc
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
from math import dist | |
from random import shuffle | |
from typing import Any | |
class Person: | |
def __init__(self, name, questions : tuple): | |
self.name = name | |
self.questions = questions | |
self.isMatched = False | |
self.preferences = [] | |
self.match = None | |
def rate(self, person: Any) -> int: | |
return dist(self.questions, person.questions) | |
def prefers_over(self, person) -> bool: | |
place = self.preferences.index(self.getByPerson(person)) | |
current_match_place = self.preferences.index(self.getByPerson(self.match)) | |
if place < current_match_place: | |
return True | |
else: | |
return False | |
# Self.preferences is the list of people sorted by prefernce | |
# Self.match is the match | |
def checkup(self): | |
if self.isMatched == False: | |
self.match = None | |
if self.match != None: | |
self.match.match = self | |
self.match.isMatched = True | |
def getByPerson(self, person1): | |
for person in self.preferences: | |
if person[0] == person1: | |
return (person[0], person[1]) | |
def create_prefernces(self, people: list) -> list: | |
for person in people: | |
if person == self: | |
continue | |
rating = self.rate(person) | |
self.preferences.append((person, rating)) | |
self.preferences.sort(key=lambda x: x[1]) | |
return self.preferences | |
def __str__(self): | |
return str(self.name) | |
import csv | |
csvfile = csv.reader(open("responses.csv", "r")) | |
people = [] | |
# Iterate over the rows of the CSV file | |
for row in csvfile: | |
if csvfile.line_num == 1: | |
continue | |
# Create a new Person object with the data from the row | |
questions = tuple([int(i) for i in row[2:]]) | |
person = Person(row[1], questions) | |
# print(questions) | |
# Print out the data in the object | |
people.append(person) | |
shuffle(people) | |
middle_index = len(people) // 2 | |
first_half = people[:middle_index] | |
second_half = people[middle_index:] | |
assert len(first_half) == len(second_half) | |
for person in first_half: | |
person.create_prefernces(second_half) | |
for person in second_half: | |
person.create_prefernces(first_half) | |
def has_unmatched(people: list) -> bool: | |
for person in people: | |
if person.isMatched == False: | |
return True | |
return False | |
def get_first_unmatched(people: list) -> Person: | |
for person in people: | |
if person.isMatched == False: | |
return person | |
def count_unmatches(people: list) -> int: | |
count = 0 | |
for person in people: | |
if person.isMatched == False: | |
count += 1 | |
return count | |
while has_unmatched(people): | |
person = get_first_unmatched(first_half) | |
if count_unmatches(second_half) == 0 and count_unmatches(first_half) > 0: | |
print("WTF") | |
exit() | |
for prefrence in person.preferences: | |
current_match = prefrence[0] | |
if current_match.isMatched == False: | |
current_match.isMatched = True | |
person.isMatched = True | |
person.match = current_match | |
current_match.match = person | |
break | |
else: | |
if current_match.prefers_over(person): | |
old_match = current_match.getByPerson(current_match.match)[0] | |
if old_match: | |
old_match.isMatched = False | |
old_match.match = None | |
current_match.match = person | |
person.match = current_match | |
person.isMatched = True | |
current_match.isMatched = True | |
break | |
else: | |
continue | |
for person in first_half: | |
print(person.name, "->", person.match.name, round(person.rate(person.match), 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment