parent
c9b3d2278d
commit
7c46e612f4
@ -0,0 +1,8 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="MaterialThemeProjectNewConfig">
|
||||
<option name="metadata">
|
||||
<MTProjectMetadataState>
|
||||
<option name="migrated" value="true" />
|
||||
<option name="pristineConfig" value="false" />
|
||||
<option name="userId" value="-7041df4:192edc56d40:-7ffa" />
|
||||
</MTProjectMetadataState>
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
||||
@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings" defaultProject="true" />
|
||||
</project>
|
||||
|
@ -0,0 +1,72 @@
|
||||
import csv
|
||||
from random import shuffle
|
||||
|
||||
# 1. Load participants from CSV, now including id and last_match_id
|
||||
def load_participants(filename):
|
||||
participants = []
|
||||
with open(filename, 'r') as file:
|
||||
reader = csv.DictReader(file)
|
||||
for row in reader:
|
||||
participants.append({
|
||||
'id': int(row['id']),
|
||||
'name': row['name'],
|
||||
'household': row['household'],
|
||||
'email': row['email'],
|
||||
'last_match_id': int(row['last_match_id']) if row['last_match_id'] else None
|
||||
})
|
||||
return participants
|
||||
|
||||
# 2. Match participants with conditions, avoiding self-gift and reciprocal matches
|
||||
def match_participants(participants):
|
||||
shuffled = participants.copy()
|
||||
shuffle(shuffled) # Randomize list
|
||||
|
||||
matches = {}
|
||||
for giver in participants:
|
||||
for receiver in shuffled:
|
||||
if giver['id'] != receiver['id'] and \
|
||||
giver['household'] != receiver['household'] and \
|
||||
giver['last_match_id'] != receiver['id'] and \
|
||||
matches.get(receiver['id']) != giver['id']: # Avoid reciprocal match
|
||||
matches[giver['id']] = receiver
|
||||
shuffled.remove(receiver)
|
||||
break
|
||||
return matches
|
||||
|
||||
# 3. Update participants with this year's matches
|
||||
def update_last_match_in_file(participants, matches, filename):
|
||||
for giver_id, receiver in matches.items():
|
||||
for participant in participants:
|
||||
if participant['id'] == giver_id:
|
||||
participant['last_match_id'] = receiver['id']
|
||||
|
||||
# Write updated participants back to CSV
|
||||
with open(filename, 'w', newline='') as file:
|
||||
fieldnames = ['id', 'name', 'household', 'email', 'last_match_id']
|
||||
writer = csv.DictWriter(file, fieldnames=fieldnames)
|
||||
writer.writeheader()
|
||||
for participant in participants:
|
||||
writer.writerow({
|
||||
'id': participant['id'],
|
||||
'name': participant['name'],
|
||||
'household': participant['household'],
|
||||
'email': participant['email'],
|
||||
'last_match_id': participant['last_match_id']
|
||||
})
|
||||
|
||||
# 4. Main function to run the match and update the file
|
||||
def main():
|
||||
participants = load_participants('participants.csv')
|
||||
matches = match_participants(participants)
|
||||
|
||||
# Print and update matches
|
||||
for giver_id, receiver in matches.items():
|
||||
giver = next(p for p in participants if p['id'] == giver_id)
|
||||
print(f"{giver['name']} ({giver['email']}) will give a gift to {receiver['name']} ({receiver['email']})")
|
||||
|
||||
# Update last matches in the CSV file
|
||||
update_last_match_in_file(participants, matches, 'participants.csv')
|
||||
print("Participants file updated with this year's matches.")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Reference in new issue