gpt4 book ai didi

Python随机运动时间表生成器

转载 作者:太空狗 更新时间:2023-10-30 03:06:01 25 4
gpt4 key购买 nike

我发现了以下内容

Generating natural schedule for a sports league

每次运行它都会生成相同的时间表。如果我在其中添加一个 random.shuffle() ,它仍然太容易预测了。

下面是我对该帖子进行的简单编辑,其中有一个随机位置,我得到了奇怪的结果

import random

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 = range(5)
random.shuffle(teams)
schedule = list(round_robin(teams, sets = len(teams) * 2 - 2))

for week in schedule:
for game in week:
if 0 in game:
print game

有时似乎球队甚至不会互相比赛..看看结果

(0, 4)
(4, 0)
(0, 2)
(0, 4)
(4, 0)
(0, 2)

我的问题是.. 我怎样才能在 python 中做一个随机计划生成器或修复我已经拥有的东西。

我几乎需要带 4 支球队进行 3 周的比赛,他们之间只打一次比赛。或者像 5 支球队,其中有 5 周的比赛,他们都互相比赛一次,但每周有一支球队被排除在外

最佳答案

我假设每个团队都需要与其他团队一起比赛。不是吗?那这道题不就归结为简单的排列,然后把结果洗牌了吗?

import itertools
import random
set_size = 2
schedule = set()
teams = range(5)
for comb in itertools.product(teams, repeat=set_size):
comb = sorted(list(comb))
if len(set(comb)) == set_size:
schedule.add(tuple(comb))

schedule = list(schedule)
random.shuffle(schedule)
print schedule

关于Python随机运动时间表生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9900839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com