Skip to content

Instantly share code, notes, and snippets.

@ethanfuerst
Created September 25, 2023 02:48
Show Gist options
  • Save ethanfuerst/1f0b577188d1806b0414f20952b4929e to your computer and use it in GitHub Desktop.
Save ethanfuerst/1f0b577188d1806b0414f20952b4929e to your computer and use it in GitHub Desktop.
Brute forcing my way through gift exchanges
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