Problem

Find all the combination of two in a team

Solution

from itertools import combinations

team = ["john", "joe", "bob", "al", "tony", "mark"]
comb = combinations(team, 2)

for i in list(comb):
    print(i)

Result:

('john', 'joe')
('john', 'bob')
('john', 'al')
('john', 'tony')
('john', 'mark')
('joe', 'bob')
('joe', 'al')
('joe', 'tony')
('joe', 'mark')
('bob', 'al')
('bob', 'tony')
('bob', 'mark')
('al', 'tony')
('al', 'mark')
('tony', 'mark')

Let’s up the game

now we want to arrange the combinations results in a way that none of these guys are on call twice in a row

  • Step 1: put people 2 by 2 in a list


from itertools import combinations
import random

team = ["john", "joe", "bob", "al", "tony", "mark"]
comb = combinations(team, 2)

oncall = []

for i in list(comb):
    oncall.append(i)

random.shuffle(oncall)

print(oncall)

Result:

: [('bob', 'tony'), ('tony', 'mark'), ('bob', 'al'), ('al', 'tony'), ('bob', 'mark'), ('joe', 'al'), ('john', 'mark'), ('john', 'tony'), ('joe', 'tony'), ('joe', 'bob'), ('john', 'joe'), ('joe', 'mark'), ('al', 'mark'), ('john', 'al'), ('john', 'bob')]
  • Step 2: iterate this list an pickup people at least spaced out by len(list)/2 - 1


oncall_options =  [('bob', 'tony'), ('tony', 'mark'), ('bob', 'al'), ('al', 'tony'), ('bob', 'mark'), ('joe', 'al'), ('john', 'mark'), ('john', 'tony'), ('joe', 'tony'), ('joe', 'bob'), ('john', 'joe'), ('joe', 'mark'), ('al', 'mark'), ('john', 'al'), ('john', 'bob')]

oncall = [('bob','tony')]

def overlap(t1, t2):
    check = any(item in t1 for item in t2)
    return check

i = 0
for j in oncall_options:
    if not overlap(oncall[i],j):
        oncall.append(j)
        i = i + 1

print(oncall)

Result:

 [('bob', 'tony'), ('joe', 'al'), ('john', 'mark'), ('joe', 'tony'), ('al', 'mark'), ('john', 'bob')]

This does not work til the end. But for the current needs it fits. It will give a basic scheduled team for a few weeks.

I am pretty sure we can find some nicer stuff using grah exploration. My leftover of on graph are quite far away, I’ll try go back in there and see if I can dig some stuff.

If you feel like you can contribute, feel free to PR/Comment on github, on this repo.