Generating natural schedule for a sports league

前端 未结 2 955
醉话见心
醉话见心 2020-12-06 05:42

I\'m looking for an algorithm to generate a schedule for a set of teams. For example, imagine a sports season in which each team plays each other, one time as home team and

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-06 06:12

    I found a method here which I adapted slightly to this:

    def round_robin(units, sets = None):
        """ Generates a schedule of "fair" pairings from a list of units """
        count = len(units)
        sets = sets or (count - 1)
        half = count / 2
        for turn in range(sets):
            left = units[:half]
            right = units[count - half - 1 + 1:][::-1]
            pairings = zip(left, right)
            if turn % 2 == 1:
                pairings = [(y, x) for (x, y) in pairings]
            units.insert(1, units.pop())
            yield pairings
    
    teams = ['a', 'b', 'c', 'd']
    print list(round_robin(teams, sets = len(teams) * 2 - 2))
    

    Now I just need to turn this into plpgsql. :)

提交回复
热议问题