【问题标题】:Need help creating league schedule based on matchup table需要帮助根据对决表创建联赛时间表
【发布时间】:2018-08-21 01:48:02
【问题描述】:

我有一张桌子,上面有一组球队,以及他们需要与其他球队进行多少场比赛,如下所示:

在此表中,a、b、c 和 d 队各打两场,另一组各打一场,每队共打 10 场比赛。我需要根据此表创建每周对战时间表(例如:第 1 周 - a vs b、c vs d 等),以便所有比赛将在 10 周内进行,每支球队每周进行一场比赛.换句话说,我需要知道每支球队在 10 周内每周打哪支球队,以便每支球队打 10 场比赛。没有要考虑的家/外地。

编辑:还有一点需要注意的是,每支球队将在每周的同一天比赛,所以基本上将这些比赛安排在 10 天之内,每支球队在 10 天的每一天都打一场比赛。

我很难找到一个好的方法来做到这一点,所以如果有人对算法/包有任何建议或者可以向我指出一些资源(我的首选语言是 python),我将非常感激。

提前致谢!

【问题讨论】:

    标签: python algorithm scheduling sports-league-scheduling-problem


    【解决方案1】:

    一种非常简单的方法是生成组合,然后添加双打,如下所示:

    from itertools import combinations
    
    teams = "abcdefgh"
    
    possible_games = combinations(teams, 2)
    
    doubles = {"a":("b", "c", "d"), "b":("c", "d"), "c":("d",), "e":("f", "g", "h"), "f":("g", "h"), "g":("h",)}
    all_doubles = {(k, v) for k, values in doubles.iteritems() for v in values}
    print all_doubles
    
    games = list(possible_games)
    games += [g for g in games if g in all_doubles]
    print games, len(games)
    

    这留下了关于实际日程安排的问题 - 您需要确保(我相信)例如 A 队不会连续 7 天比赛然后闲置一周,但以上应该是一个好的开始我想。

    【讨论】:

    • 我觉得games += [g for g in games if g in all_doubles]可以简化成games += list(all_doubles)
    【解决方案2】:

    您可以在前 7 天进行第一轮循环赛,这样每支球队都会与其他球队交手一次。然后在最后3天,做两个单独的循环赛,让abcd中的队伍再次对战,efgh中的队伍再次对战。 更多关于循环赛的信息:

    # a table is a simple list of teams
    def next_table(table):
      return [table[0]] + [table[-1]] + table[1:-1]
      # [0 1 2 3 4 5 6 7] -> [0 7 1 2 3 4 5 6]
    
    # a pairing is a list of pairs of teams
    def pairing_from_table(table):
      return list(zip(table[:len(table)//2], table[-1:len(table)//2-1:-1]))
      # [0 1 2 3 4 5 6 7] -> [(0,7), (1,6), (2,5), (3,4)]
    
    # a team is an element of the table
    def round_robin(table):
      pairing_list = []
      for day in range(len(table) - 1):
        pairing_list.append(pairing_from_table(table))
        table = next_table(table)
      return pairing_list
    
    def get_programme():
      first7days = round_robin(list('abcdefgh'))
      last3days_abcd = round_robin(list('abcd'))
      last3days_efgh = round_robin(list('efgh'))
      return first7days + [a+e for a,e in zip(last3days_abcd, last3days_efgh)]
    
    print(get_programme())
    # [[('a', 'h'), ('b', 'g'), ('c', 'f'), ('d', 'e')],
    #  [('a', 'g'), ('h', 'f'), ('b', 'e'), ('c', 'd')],
    #  [('a', 'f'), ('g', 'e'), ('h', 'd'), ('b', 'c')],
    #  [('a', 'e'), ('f', 'd'), ('g', 'c'), ('h', 'b')],
    #  [('a', 'd'), ('e', 'c'), ('f', 'b'), ('g', 'h')],
    #  [('a', 'c'), ('d', 'b'), ('e', 'h'), ('f', 'g')],
    #  [('a', 'b'), ('c', 'h'), ('d', 'g'), ('e', 'f')],
    #  [('a', 'd'), ('b', 'c'), ('e', 'h'), ('f', 'g')],
    #  [('a', 'c'), ('d', 'b'), ('e', 'g'), ('h', 'f')],
    #  [('a', 'b'), ('c', 'd'), ('e', 'f'), ('g', 'h')]]
    

    【讨论】:

      【解决方案3】:

      要实现循环赛,请创建包含所有游戏的第一轮。想象一下,上一场比赛中除了主队之外的所有球队都像链条一样相连。顺时针旋转这条链(总共 nr 个团队减去 2 次) 像这样

      第一轮

      第 1 队 - 第 2 队 | | 第 5 队 - 第 3 队 \ | 第 6 队 - 第 4 队

      第二轮

      第 5 队 - 第 1 队 | | 第 4 队 - 第 2 队 \ | 第 6 队 - 第 3 队

      等等。 适用于所有数量的团队。如果是奇数队,只需删除每轮的最后一场比赛。 由你来编写算法。我很久以前就做过了。

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多