我不会尝试正确地计算分数,而只是一次以适当的比例分配目标。在这里,“
allocate_goals”生成器将目标分配给每个低比率目标,然后分配给每个较高比率目标(重复3次)。然后重复。调用方inalloc使用itertools.islice以所需的数量(玩家数量)切断此无限生成器。
import collectionsimport itertoolsimport stringdef allocate_goals(prop_low, prop_high): prop_high3 = prop_high * 3 while True: for g in prop_low: yield g for g in prop_high3: yield gdef allocate(goals, players): letters = string.ascii_uppercase[:goals] high_count = goals // 2 prop_high, prop_low = letters[:high_count], letters[high_count:] g = allocate_goals(prop_low, prop_high) return collections.Counter(itertools.islice(g, players))for goals in xrange(2, 9): print goals, sorted(allocate(goals, 8).items())
它产生以下答案:
2 [('A', 6), ('B', 2)]3 [('A', 4), ('B', 2), ('C', 2)]4 [('A', 3), ('B', 3), ('C', 1), ('D', 1)]5 [('A', 3), ('B', 2), ('C', 1), ('D', 1), ('E', 1)]6 [('A', 2), ('B', 2), ('C', 1), ('D', 1), ('E', 1), ('F', 1)]7 [('A', 2), ('B', 1), ('C', 1), ('D', 1), ('E', 1), ('F', 1), ('G', 1)]8 [('A', 1), ('B', 1), ('C', 1), ('D', 1), ('E', 1), ('F', 1), ('G', 1), ('H', 1)]
这种方法的优点(除了我认为,这很容易理解)是,它很快可以将其转换为随机版本。
只需用以下命令替换allocate_goals:
def allocate_goals(prop_low, prop_high): all_goals = prop_low + prop_high * 3 while True: yield random.choice(all_goals)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)