保龄球时间表

2024-06-26 00:23:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我在为保龄球比赛做计划。我的问题是有些保龄球手共用一个私人保龄球。所以保龄球号码相同的保龄球手不可能在同一个回合。你知道吗

我可以编一本这样的字典:

dict = {1:None,2:1,3:None,4:None,5:None,6:1,7:2,8:2,9:3,10:None}

dict={bowler\u id,shared\u ball}(None=无共享球)

这是我的第一次尝试,有问题:

from operator import itemgetter

bowlers = {1:None,2:1,3:None,4:None,5:None,6:1,7:2,8:2,9:3,10:None,11:None,12:None,13:None,14:None,15:None,16:1,17:None,18:None,19:None,20:None,21:2,22:3,23:None}
Lanes = 6
Rounds = 6
Schedule = {}

sortedlist = sorted(bowlers.items(), key=itemgetter(1), reverse=True)

for x in xrange(1,Lanes+1):
    Schedule[''.join(['Lane', str(x)])] = []

while len(sortedlist) > 0:
    z = zip(Schedule,sortedlist)
    for i in z:
        Schedule[i[0]].append((i[1][0] , i[1][1]))
        sortedlist.pop(0)
print Schedule 

我有这些问题/顾虑:

这种方法的作用是相反的:因为在一个排序的列表上输入,同一个球的投手在同一回合中得到保证。你知道吗

有人能给我指出正确的方向吗?你知道吗

程序的输出为:

   {    
    'Lane6': [(9, 3), (6, 1), (10, None), (17, None)], 
    'Lane5': [(22, 3), (16, 1), (11, None), (18, None)], 
    'Lane4': [(7, 2), (1, None), (12, None), (19, None)], 
    'Lane3': [(8, 2), (3, None), (13, None), (20, None)], 
    'Lane2': [(21, 2), (4, None), (14, None), (23, None)], 
    'Lane1': [(2, 1), (5, None), (15, None)]
}

每个纵队都是所有车道上的转弯处。当您查看第一列时,您会看到共享球3和2在一列中出现的次数多于一次。这是错误的,因为你不能有两个投球手在同一个球在一个回合。你知道吗

正确的输出如下:

{   
    'Lane6': [(10, None),   (9, 3),     (6, 1),     (17, None)], 
    'Lane5': [(22, 3),      (16, 1),    (11, None), (18, None)], 
    'Lane4': [(7, 2),       (1, None),  (12, None), (19, None)], 
    'Lane3': [(3, None),    (13, None), (8, 2),     (20, None)], 
    'Lane2': [(14, None),   (21, 2),    (4, None),  (23, None)], 
    'Lane1': [(2, 1),       (5, None),  (15, None)]
}

投球手的顺序可能是兰登,只要一个回合内没有共用球。你知道吗


Tags: innonefordictschedule共用sortedlistitemgetter
1条回答
网友
1楼 · 发布于 2024-06-26 00:23:17

谢谢你的挑战!你知道吗

我重新命名了一些标识符以使其更有意义,而且我已经更改了 球None到球0,使输出更整洁。你知道吗

这个程序根据投球手使用的球来分组,然后 每组最多选一个,再加上任何数量的投球手 不要共用一个球。你知道吗

from copy import deepcopy
from random import shuffle
from json import dumps     # for pretty printing of results

bowlers2balls = {
    1:0,2:1,3:0,4:0,5:0,6:1,7:2,8:2,9:3,10:0,11:0,12:0,13:0,
    14:0,15:0,16:1,17:0,18:0,19:0,20:0,21:2,22:3,23:0
}

Lanes = 6
Schedule = {}
min_turns = (len(bowlers2balls) - 1) / Lanes + 1

# Create a list of lists. Bowlers with a shared ball are all in the
# same sublist. Each bowler without a shared ball gets their own
# sublist. Ball numbers do not appear here.

shared = [[tup[0] for tup in bowlers2balls.items() if tup[1] == ball]
          for ball in set(bowlers2balls.values()) if ball]
for grp in shared:
    shuffle(grp)
unshared = [[tup[0]] for tup in bowlers2balls.items() if not tup[1]]
full_bgroups = shared + unshared

# Generate random schedules until we get one with minimum turns. It
# often happens that we get a suboptimal one because the bowlers with
# shared balls aren't used up.

while True:
    for lane in xrange(1,Lanes+1):
        Schedule['Lane{}'.format(lane)] = []

    bgroups = deepcopy(full_bgroups)
    while bgroups:
        shuffle(bgroups)
        for i, lane in enumerate(Schedule.keys()):
            if i >= len(bgroups):
                break
            bowler = bgroups[i].pop()
            Schedule[lane].append(
                ('{:2d}'.format(bowler),
                 bowlers2balls[bowler]))
        # Remove emptied lists from bgroups
        bgroups = filter(None, bgroups)

    turns = max([len(s) for s in Schedule.values()])
    if turns <= min_turns:
        break

print (dumps(Schedule, sort_keys=True)
       .strip('{}')
       .replace(']], ', ']],\n'))

相关问题 更多 >