当前位置: 首页 > 知识库问答 >
问题:

条件循环橄榄球抽签的列表操作

左丘智渊
2023-03-14

我学校的一位橄榄球教练让我为即将到来的比赛编写一份有条件的橄榄球抽签,任务如下:给出一份从1-12分为3组的球队名单([Group1=1,2,3,4],[Group2=5,6,7,8],[Group3=9,10,11,12])生成并打印具有以下条件的11轮比赛:

>

  • 第1组中的团队不与第3组中的团队对抗
  • 第1组中的团队与第1组中的其他团队对比两次(例如1v2、2v1、1v3、3v1、1v4、4v1、1v5、5v1……)

    这一规则同样适用于第3组中的团队,就像他们与第3组中的其他团队一样

    我尝试了多次,但不可避免地陷入困境,以下是我的两次尝试:

    尝试1:

    import operator
    import functools
    import random
    
    
    
    ###First Generation (Flawed unclean round robin)
    def fixtures(teams):
        if len(teams) % 2:
            teams.append('Day off')  # if team number is odd - use 'day off' as fake team     
    
        rotation = list(teams)       # copy the list
        random.shuffle(rotation)
    
        fixtures = []
        for i in range(0, len(teams)-1):
            fixtures.append(rotation)
            rotation = [rotation[0]] + [rotation[-1]] + rotation[1:-1]
    
        return fixtures
    
    def main():
        # demo code
        teams = ["Team1","Team2","Team3","Team4","Team5","Team6","Team7","Team8","Team9","Team10","Team11","Team12"]
        groupA = ["Team1","Team2","Team3","Team4"]
        groupB = ["Team5","Team6","Team7","Team8"]
        groupC = ["Team9","Team10","Team11","Team12"]
    
        # for one match each - use this block only
        matches = fixtures(teams)
    
        print("flawed matches:")
        RoundCounter = 0
    
        homeTeams = []
        awayTeams = []
    
        for f in matches:
            #print(f)
            homeTeams = f[::2]
            awayTeams = f[1::2]
            print("Home Teams:{}".format(homeTeams))
            print("Away Teams:{}".format(awayTeams))
            HomeTeamGroupA = set(homeTeams).intersection(groupA)
            HomeTeamGroupC = set(homeTeams).intersection(groupC)
            AwayTeamGroupA = set(awayTeams).intersection(groupA)
            AwayTeamGroupC = set(awayTeams).intersection(groupC)
    
            VSCounter = 0
    
            for p, o in zip(homeTeams, awayTeams):
                if p in HomeTeamGroupA:
                    if o in AwayTeamGroupC:
                        AvsCPosition = awayTeams.index(o)
                        VSCounter += 1
                        RoundCleanUp(homeTeams, awayTeams, AvsCPosition, VSCounter) #if this is returned begin cleaning the round
                    else: print("GroupA is versing either Group B or GroupA") #if this is returned it is a team 1-4 but is vs either group b or group a
                elif p in HomeTeamGroupC:
                    if o in AwayTeamGroupA:
                        AvsCPosition = awayTeams.index(o)
                        VSCounter += 1
                        RoundCleanUp(homeTeams, awayTeams, AvsCPosition, VSCounter) #if this is returned begin cleaning the round
                    else:
                        print("GroupC is versing either Group B or GroupC")  #if this is returned it is a team 9-12 but is vs either group b or group c
                else:
                    pass
    
    def RoundCleanUp(HTeam, ATeam, AvsCPos, VSCounter):
        ##gets Value of List at position
        HTeamVal = HTeam[AvsCPos]
        ATeamVal = ATeam[AvsCPos]
    main()
    
    

    尝试2:

    import operator
    import functools
    import random
    
    
    def make_round(rotation, num_teams, fixtures):
        for i in range(num_teams - 1):
            rotation = list(range(1, num_teams + 1))
            # clip to 0 .. num_teams - 2 # if i == 0, no rotation is needed (and using -0 as list index will cause problems)
            i %= (num_teams - 1)
            if i:
                rotation = rotation[:1] + rotation[-i:] + rotation[1:-i]
            half = num_teams // 2
            fixtures.append(list(rotation[:half]))
            fixtures.append(list(rotation[half:][::-1]))
        return fixtures
    
    
    def make_schedule(teams):
        """Produces RoundRobin"""
        # number of teams must be even
        TeamLength = len(teams)
        if TeamLength % 2:
            TeamLength += 1  # add a dummy team for padding
    
        # build first round-robin
        rotation = list(teams)
        Fixture = []
        schedule = make_round(rotation, TeamLength, Fixture)
    
        return schedule
    
    
    def homeAwayRotation(matches):
        for homeTeams, awayTeams in zip(matches[0::2], matches[1::2]):
            print("Home Rotation: {}".format(homeTeams))
            print("Away Rotation: {}".format(awayTeams))
            validation(homeTeams, awayTeams)
    
    
    def validation(homeTeams, awayTeams):
        groupA = [1, 2, 3, 4]
        groupC = [9, 10, 11, 12]
    
    
        for x, y in zip(homeTeams, awayTeams):
            if x in groupA:
                if y in groupC:
                    AvsCPosition = awayTeams.index(y)
                    cleanDirtyData(homeTeams, awayTeams, AvsCPosition)
                else:
                        # if this is returned it is a team 1-4 but is vs either group b or group a
                    print("Group A vsing either itself or GroupB\n")
            elif x in groupC:
                if y in groupA:
                    AvsCPosition = awayTeams.index(y)
                    cleanDirtyData(homeTeams, awayTeams, AvsCPosition)
                else:
                    # if this is returned it is a team 9-12 but is vs either group b or group c
                    print("Group C vsing either itself or GroupB\n")
            else:
                # if this is returned it is a team in group B
                print("This is team B\n")
    
    
    def cleanDirtyData(homeTeams, awayTeams, AvsCPosition):
        HTeamVal = homeTeams[AvsCPosition]
        ATeamVal = awayTeams[AvsCPosition]
        Dirtlist = []
        Dirtlist.append(HTeamVal)
        Dirtlist.append(ATeamVal)
    def main():
        # demo code
        teams = ["Team1", "Team2", "Team3", "Team4", "Team5", "Team6",
                 "Team7", "Team8", "Team9", "Team10", "Team11", "Team12"]
    
        # for one match each - use this block only
        matches = make_schedule(teams)
    
        print("flawed matches:")
    
        homeAwayRotation(matches)
    
    
    main()
    

    我的预期结果是打印每一轮,显示哪个队在驾驶哪个队,每个队的历史有点像这样:

    >

  • 第一组中的一个团队的诗史为:(任意顺序)

    1v2,2v1,1v3,3v1,1v4,4v1,1v5,1v6,1v7,1v8,再见

    Group2中的一个团队有以下诗句历史:(以任意随机顺序)

    5v1,5v2,5v3,5v4,5v6,5v7,5v8,5v9 5v10,5v11,5v12

    Group3中的一个团队有以下诗句历史:(以任意随机顺序)

    9v10,10v9,9v11,11v9,9v12,12v9,9v5,9v6,9v7,9v8,再见

    如果我能提出任何建议或改进,我将不胜感激,因为在过去的两周里,我一直被困在最后的障碍上

  • 共有1个答案

    扶冠宇
    2023-03-14

    如果我对问题的理解是正确的,那么您所需要的就是将团队与不同团队中的每个成员结合起来。

    我编写了一些代码来解决您的问题:

    def vs(team, group):
        matchups = map(lambda opponent: (team,opponent), group)
        matchups = filter(lambda tup: tup[0] != tup[1], matchups)
        return list(matchups)
    
    def matches(teams):
        group_size = len(teams) // 3
    
        # Make the groups, basically just splitting the team list in three parts
        groups = [teams[:group_size], teams[group_size:2*group_size], teams[2*group_size:]]
        matchups = []
        for index, team in enumerate(teams):
            group_index = index // group_size
            current_matchup = []
    
            # Check if we're working with group 1 or 3
            if group_index == 0 or group_index == 2:
    
                # Flip the order of a tuple
                def flip(x):
                    return (x[1], x[0])
    
                own_group = vs(team, groups[group_index])
    
                # Add matches against everyone in the group
                current_matchup.extend(own_group)
    
                # Add matches agains everyone in the group again, but now the current team is 'away'
                current_matchup.extend(list(map(flip, own_group)))
    
                # Add matches against everyone in group 2
                current_matchup.extend(vs(team, groups[1]))
    
                # Lastly, add the bye 
                current_matchup.append((team, "bye"))
            else:
                # Just all matches against all other teams, once.
                current_matchup.extend(vs(team, teams))
            matchups.append(current_matchup)
        return matchups
    
    # This can be anything. Numbers, 'Team 1' or even 'The wondrous flying squirrels of death'
    teams = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    # Make matches
    matchups = matches(teams)
    
    
    # Just pretty print
    for i in range(len(matchups)):
        matches = '\n\t'.join(map(lambda m: f'{str(m[0]).rjust(10)} vs {str(m[1]).ljust(10)}', matchups[i]))
        print(f"Team '{teams[i]}' matches:\n\t{matches}")
    
     类似资料:
    • 3.2.1 无循环变量的DO构造 a) 一般形式 这种DO构造形式非常简单,一般形式为: [构造名:] DO 块 END DO 不带循环控制变量的DO构造控制机制为:进入DO构造体后,从DO语句下面第一句执行起顺次执行到END DO前的最后一句,再返上来从DO语句下面第一句执行起,重复执行整个DO块。如此反复执行DO块,其间如遇到EXIT语句,就停止执行DO块,退出循环,转向执行END DO下面的

    • 控制切换一个元素是否显示也相当简单: <div id="app-3"> <p v-if="seen">现在你看到我了</p> </div> var app3 = new Vue({ el: '#app-3', data: { seen: true } }) 现在你看到我了 继续在控制台输入app3.seen = false,你会发现之前显示的消息消失了。 这个例子演示

    • 编辑:重新措辞 我有一个账户列表:帐户 当我总结这份清单时,我得到了账户数量和账户总价值的概述 名称摘要 我添加了马克,因为他现在将是一个帐户所有者,但他目前没有任何帐户。 第一步是查看每个人应该拥有的账户的平均数。有25个账户,5个所有者,所以每个人都应该有5个账户。总价值为15600英镑,共有5名所有者,因此每个人的账户价值应该在3120英镑左右。这是一个估计值,所以我对它稍微偏离一点没意见。

    • 我有一个具有字段is和is_searchable data has的列表

    • 本章的主题是if语句,就是条件判断,会对应程序的不同状态来执行不同的代码。但首先我要介绍两种新的运算符:floor(地板除法,舍弃小数位)和modulus(求模,取余数) 5.1 地板除和求模 floor除法,中文有一种翻译是地板除法,挺难听,不过凑活了,运算符是两个右斜杠://,与传统除法不同,地板除法会把运算结果的小数位舍弃,返回整值。例如,加入一部电影的时间长度是105分钟。你可能想要知道这

    • Q.while循环条件是作为一个整体进行计算,还是在决定是否进入循环之前一次计算一个条件?