Created
September 25, 2023 02:48
-
-
Save ethanfuerst/1f0b577188d1806b0414f20952b4929e to your computer and use it in GitHub Desktop.
Brute forcing my way through gift exchanges
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
import pandas as pd | |
import random | |
random.seed(0) | |
# giver, family, recipient_year1, ... ,recipient_yearN | |
df = pd.read_csv("list.csv") | |
def in_same_family(pair): | |
return ( | |
df[df["giver"] == pair[0]]["family"].iloc[0] | |
== df[df["giver"] == pair[1]]["family"].iloc[0] | |
) | |
def has_given_to_before(pair): | |
given_to = df[df["giver"] == pair[0]].iloc[:, 2:].values | |
return pair[1] in given_to | |
def is_same_person(pair): | |
return pair[0] == pair[1] | |
def pair_does_not_work(pair): | |
return is_same_person(pair) or in_same_family(pair) or has_given_to_before(pair) | |
def main(): | |
conditions_met = False | |
givers = df["giver"].values.copy() | |
recipients = df["giver"].values.copy() | |
while not conditions_met: | |
random.shuffle(recipients) | |
for pair in zip(givers, recipients): | |
if pair_does_not_work(pair): | |
break | |
else: | |
print("Conditions met!") | |
conditions_met = True | |
for i in zip(givers, recipients): | |
print(f"{i[0]} gives to {i[1]}") | |
df["recipient_2023"] = recipients | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment