[Python] Find ternary relationships with no duplicates for game scheduling algorithm

Discussão em 'Python' iniciado por Stack, Setembro 2, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on assigning groups in Python using rules.

    You could think about it like building a season for a three player based game.

    Each player needs to face the other players on the list exactly once.

    So suppose I have this list:


    players = ['a','b','c','d','e','f','g','h','i','j','k']



    Here's the expected output if we start with 'a':

    game schedule for a:

    game 1 = a, b, c

    game 2 = a, d, e

    game 3 = a, f, g

    game 4 = a, h, i

    game 5 = a, j, k

    I'll need a schedule like that above for each player.

    What I've got right now gets me 45 combinations for player 'a'... trying to figure out how to whittle it down:


    players = ['a','b','c','d','e','f','g','h','i','j','k']

    from itertools import combinations

    num = 3

    all_games = list(combinations(players, num))

    a_s = []

    for i in all_games:
    if 'a' in i:
    a_s.append(i)



    I'm just not sure how to build in the condition I want.

    I suppose I could (in pseudo code):


    create a list without the player I want:

    player = a
    opponents = ['b','c','d','e','f','g','h','i','j','k']

    extract/get every 2 elements from the list

    add player a to the extracted opponent pairs



    But I don't know if that's close to the best way to solve this.

    Continue reading...

Compartilhe esta Página