[LM] initial commit

main
Lukas Martin 2 years ago
parent c9b3d2278d
commit 7c46e612f4

8
.idea/.gitignore vendored

@ -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,11 @@
id,name,household,email,last_match_id
0,Lukas,Martin,contact@lukasmartin.eu,4
1,Jan,Martin,Null,3
2,Ulrike,Martin,Null,5
3,Petra,Scheib,Null,2
4,Dietmar,Scheib,Null,1
5,Lisa,Scheib,Null,8
6,Andreas,Scheib,Null,0
7,Annette,Simon,Null,6
8,Julia,Simon,Null,3
9,Marc,Simon,Null,6
1 id name household email last_match_id
2 0 Lukas Martin contact@lukasmartin.eu 4
3 1 Jan Martin Null 3
4 2 Ulrike Martin Null 5
5 3 Petra Scheib Null 2
6 4 Dietmar Scheib Null 1
7 5 Lisa Scheib Null 8
8 6 Andreas Scheib Null 0
9 7 Annette Simon Null 6
10 8 Julia Simon Null 3
11 9 Marc Simon Null 6

@ -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…
Cancel
Save