While I understand that a round-robin algorithm can be easily used for one-off flows, I need to create a round-robin algorithm which references the results of previous runs, and ensures no duplication of matches over long periods of time.
I am trying to create a script which takes a list of names from a Google Sheet, randomly matches two names at a time, schedules a meeting for those two people and writes the pairing back to the Sheet to keep a history, to make sure the pairing doesn't happen the next time the script is run.
It's effectively a 'round robin' concept where each person is randomly matched with every other person only once. The script runs ~once a week to create a meeting every week.
Here is the overview of the flow:
- Pull list of names from Sheet
- Randomly select two names to create a 'pair'
- Check this pairing hasn't happened before by referencing the Sheet with historical pairings
- If new pairing, create an event and write the pairing back to the Sheet
- If not a new pairing, cancel the pair and try again
- Keep looping until all names are paired, or until one is left/no new pairings possible
I already have code working, but it is very scrappy. My current approach is basically
names = [<names taken from Sheet via API>]
for name in names:
person1 = random.choice(names)
names.remove(person1)
person2 = random.choice(names)
names.remove(person2)
match = person1 + "," + person2
<logic for calling Sheets API with current match to make sure it hasn't previously happened>
I have much more code which does the other elements like call different APIs to GET/POST the data, but I'm certain there's a better way to do this.
Currently the script works, but it often fails after once the 'names' list gets down to the final few people, as either all of the remaining people have previously been matched with each other, or there is an odd one out, so an error is thrown which is not properly handled.