[Python] Unable to generate the combination x and combination y using python

Discussão em 'Python' iniciado por Stack, Outubro 1, 2024.

  1. Stack

    Stack Membro Participativo

    Unable to generate desired combination 2 and combination 5 Current OutPut

    Combination 1: Rutgers (93), Baylor (93), Orlando (96), Dublin (96), Spokane (96)
    Combination 2: Rutgers (89), Baylor (90), Orlando (90), Dublin (85), Spokane (85)


    Expected Output Something like this.

    COMBINATION 2: Trenton (89), Alabama (93), Orlando (96), Atlanta (96), Chicago (90)
    COMBINATION 3: Rutgers (89), Baylor (93), Miami (96), Atlanta (85), SFO (90)
    COMBINATION 4: Alabama (89), Trenton (93), Dublin (96), Chicago (96), Seattle (90)
    COMBINATION 5: Baylor (89), Rutgers (93), SFO (96), Seattle (96), Trenton (90)
    COMBINATION 6: Orlando (89), Atlanta (93), Spokane (96), Trenton (96), Alabama (85)

    .
    .
    .
    .


    These are conditions we added for the code to reach my expectations.

    -The team should have different points as per point_constraints,
    Example

    COMBINATION 2: Trenton (89), Alabama (93), Orlando (96), Atlanta (96), Chicago (90)
    COMBINATION 3: Rutgers (89), Baylor (93), Miami (96), Atlanta (85), SFO (90)


    -Pick only 1 Team from each set -5 team combination

    Criteria -

    The criteria for the combinations follows these rules:

    1. Team Selection from Different Sets:

    You pick one team from each set (A, B, C, D, E, J) for each combination. Each team in a combination must be from a different set, and no team should be repeated in the same combination.

    1. Point Constraints:

    You apply the point constraints for each score when forming combinations. The constraints indicate how many times a score can be used in the set of combinations. The constraints are:

    • "89": (0, 1) — This score can appear 0 or 1 time in a combination.
    • "93": (0, 1) — This score can appear 0 or 1 time in a combination.
    • "96": (0, 2) — This score can appear 0, 1, or 2 times in a combination.
    • "85": (0, 1) — This score can appear 0 or 1 time in a combination.
    • "90": (0, 1) — This score can appear 0 or 1 time in a combination.
    1. Combination Size:

    Each combination should consist of 5 teams, one from each set, with their associated points.

    1. Elimination of Used Points:

    Once a combination is made, the points used in that combination are eliminated from further combinations. This ensures that no points are reused from the same teams in subsequent combinations.

    1. Repeat the Process:

    After creating a valid combination, the process is repeated with the remaining teams and their available points, forming another valid combination based on the same constraints.

    CODE


    from itertools import combinations, product
    import random

    # Define the teams and scores for each set
    sets = {
    "set_A": {
    "Trenton": ["93", "96", "89", "90", "85"],
    "Rutgers": ["93", "96", "89", "90", "85"],
    },
    "set_B": {
    "Alabama": ["93", "96", "89", "90", "85"],
    "Baylor": ["93", "96", "89", "90", "85"],
    },
    "set_C": {
    "Orlando": ["93", "96", "89", "90", "85"],
    "Miami": ["93", "96", "89", "90", "85"],
    },
    "set_D": {
    "Atlanta": ["93", "96", "89", "90", "85"],
    "Dublin": ["93", "96", "89", "90", "85"],
    },
    "set_E": {
    "Chicago": ["93", "96", "89", "90", "85"],
    "SFO": ["93", "96", "89", "90", "85"],
    },
    "set_J": {
    "Seattle": ["93", "96", "89", "90", "85"],
    "Spokane": ["93", "96", "89", "90", "85"],
    }
    }

    # Define the score constraints (min, max) allowed for each score
    score_constraints = {
    "89": (0, 1),
    "93": (0, 1),
    "96": (0, 2),
    "85": (0, 1),
    "90": (0, 1),
    }

    # Function to count score occurrences in a combination
    def count_score_occurrences(selected_scores, score_constraints):
    score_counts = {score: 0 for score in score_constraints}
    for score in selected_scores:
    if score in score_counts:
    score_counts[score] += 1
    return score_counts

    # Function to validate if a combination meets the score constraints
    def is_valid_combination(score_counts, score_constraints):
    return all(min_count <= score_counts[score] <= max_count for score, (min_count, max_count) in score_constraints.items())

    # Flatten the sets to prepare for team selection
    all_teams = []
    for set_name, teams in sets.items():
    for team_name, scores in teams.items():
    all_teams.append((set_name, team_name, scores))

    # Generate combinations of 5 different teams from different sets
    valid_combinations = []
    used_scores = set() # To track scores already used

    # Limit one team from each set
    team_combinations = list(combinations(all_teams, 5))

    # Shuffle the combinations for randomness
    random.shuffle(team_combinations)

    for team_combination in team_combinations:
    # Ensure all teams in this combination come from different sets
    set_names = {team[0] for team in team_combination}
    if len(set_names) < 5:
    continue # Skip if two teams come from the same set

    # Extract the score lists for each team in the combination
    score_lists = [team[2] for team in team_combination]

    # Generate all possible score combinations for this team combination
    for score_combination in product(*score_lists):
    # Check for duplicate scores in the combination
    if any(score in used_scores for score in score_combination):
    continue # Skip if any score has already been used

    # Count occurrences of each score
    score_counts = count_score_occurrences(score_combination, score_constraints)

    # Validate the score combination
    if is_valid_combination(score_counts, score_constraints):
    # Add this combination's scores to the used scores set
    used_scores.update(score_combination)

    # Combine team names with their valid scores
    team_combination_with_scores = [
    f"{team[1]} ({score})" for team, score in zip(team_combination, score_combination)
    ]
    valid_combinations.append(team_combination_with_scores)

    # Limit the number of valid combinations to 900
    if len(valid_combinations) >= 900:
    break

    # Print the number of valid combinations found
    print(f"Number of valid combinations: {len(valid_combinations)}")

    # Write the valid combinations to a text file
    with open("swat9.txt", "w") as file:
    for idx, combination in enumerate(valid_combinations, 1):
    file.write(f"Combination {idx}: {', '.join(combination)}\n")

    print("Valid combinations written to 'oct9.txt' file.")



    Combination 1: Rutgers (93), Baylor (93), Orlando (96), Dublin (96), Spokane (96)


    Expected Output Something like this.

    COMBINATION 2: Trenton (89), Alabama (93), Orlando (96), Atlanta (96), Chicago (90)
    COMBINATION 3: Rutgers (89), Baylor (93), Miami (96), Atlanta (85), SFO (90)
    COMBINATION 4: Alabama (89), Trenton (93), Dublin (96), Chicago (96), Seattle (90)
    COMBINATION 5: Baylor (89), Rutgers (93), SFO (96), Seattle (96), Trenton (90)
    COMBINATION 6: Orlando (89), Atlanta (93), Spokane (96), Trenton (96), Alabama (85)

    .
    .
    .
    .




    Process

    sets = {
    "set_A": {
    "Trenton": ["93", "96", "89", "90", "85"],
    "Rutgers": ["93", "96", "89", "90", "85"],
    },
    "set_B": {
    "Alabama": ["93", "96", "89", "90", "85"],
    "Baylor": ["93", "96", "89", "90", "85"],
    },
    "set_C": {
    "Orlando": ["93", "96", "89", "90", "85"],
    "Miami": ["93", "96", "89", "90", "85"],
    },
    "set_D": {
    "Atlanta": ["93", "96", "89", "90", "85"],
    "Dublin": ["93", "96", "89", "90", "85"],
    },
    "set_E": {
    "Chicago": ["93", "96", "89", "90", "85"],
    "SFO": ["93", "96", "89", "90", "85"],
    },
    "set_J": {
    "Seattle": ["93", "96", "89", "90", "85"],
    "Spokane": ["93", "96", "89", "90", "85"],
    }
    }


    SET A--Trenton--93,--Trenton--96,--Trenton--89,--Trenton--90,--Trenton--85,
    SET A--Rutgers--93,--Rutgers--96,--Rutgers--89,--Rutgers--90,--Rutgers--85,
    SET B--Alabama--93,--Alabama--96,--Alabama--89,--Alabama--90,--Alabama--85,
    SET B--Baylor--93,--Baylor--96,--Baylor--89,--Baylor--90,--Baylor--85,
    SET C--Orlando--93,--Orlando--96,--Orlando--89,--Orlando--90,--Orlando--85,
    SET C--Miami--93,--Miami--96,--Miami--89,--Miami--90,--Miami--85,
    SET D--Atlanta--93,--Atlanta--96,--Atlanta--89,--Atlanta--90,--Atlanta--85,
    SET D--Dublin--93,--Dublin--96,--Dublin--89,--Dublin--90,--Dublin--85,
    SET E--Chicago--93,--Chicago--96,--Chicago--89,--Chicago--90,--Chicago--85,
    SET E--SFO--93,--SFO--96,--SFO--89,--SFO--90,--SFO--85,
    SET J--Seattle--93,--Seattle--96,--Seattle--89,--Seattle--90,--Seattle--85,
    SET J--Spokane--93,--Spokane--96,--Spokane--89,--Spokane--90,--Spokane--85,



    As per those conditions Pick 1 from each set accouding to the constraints

    point_constraints = {
    "89": (0, 1),
    "93": (0, 1),
    "96": (0, 2),
    "85": (0, 1),
    "90": (0, 1),
    }

    Create Team of 5 combinations
    COMBINATION 1- Trenton: 89,--Alabama--93, --Orlando--96, --Atlanta--96, --Chicago--90


    SET A--Trenton--93,--Trenton--96, --Trenton--90,--Trenton--85,
    SET A--Rutgers--93,--Rutgers--96,--Rutgers--89,--Rutgers--90,--Rutgers--85,
    SET B --Alabama--96,--Alabama--89,--Alabama--90,--Alabama--85,
    SET B--Baylor--93,--Baylor--96,--Baylor--89,--Baylor--90,--Baylor--85,
    SET C--Orlando--93, --Orlando--89,--Orlando--90,--Orlando--85,
    SET C--Miami--93,--Miami--96,--Miami--89,--Miami--90,--Miami--85,
    SET D--Atlanta--93, --Atlanta--89,--Atlanta--90,--Atlanta--85,
    SET D--Dublin--93,--Dublin--96,--Dublin--89,--Dublin--90,--Dublin--85,
    SET E--Chicago--93,--Chicago--96,--Chicago--89, --Chicago--85,
    SET E--SFO--93,--SFO--96,--SFO--89,--SFO--90,--SFO--85,
    SET J--Seattle--93,--Seattle--96,--Seattle--89,--Seattle--90,--Seattle--85,
    SET J--Spokane--93,--Spokane--96,--Spokane--89,--Spokane--90,--Spokane--85,


    • Remove those used Teams with POINTS

      From here again as per constraints point_constraints = { "89": (0, 1), "93": (0, 1), "96": (0, 2), "85": (0, 1), "90": (0, 1), }

    select 5 Team combinations


    • COMBINATION 2- Rutgers: 89,--Baylor--93, --Miami--96,--Atlanta--85,--SFO--90,


    • Remaining Places and their associated points SET A--Trenton--93,--Trenton--96, --Trenton--90,--Trenton--85, SET A--Rutgers--93,--Rutgers--96, --Rutgers--90,--Rutgers--85, SET B --Alabama--96,--Alabama--89,--Alabama--90,--Alabama--85, SET B --Baylor--96,--Baylor--89,--Baylor--90,--Baylor--85, SET C--Orlando--93, --Orlando--89,--Orlando--90,--Orlando--85, SET C--Miami--93, --Miami--89,--Miami--90,--Miami--85, SET D--Atlanta--93, --Atlanta--89,--Atlanta--90,
      SET D--Dublin--93,--Dublin--96,--Dublin--89,--Dublin--90,--Dublin--85, SET E--Chicago--93,--Chicago--96,--Chicago--89, --Chicago--85, SET E--SFO--93,--SFO--96,--SFO--89, --SFO--85, SET J--Seattle--93,--Seattle--96,--Seattle--89,--Seattle--90,--Seattle--85, SET J--Spokane--93,--Spokane--96,--Spokane--89,--Spokane--90,--Spokane--85,

      From here again as per constraints point_constraints = { "89": (0, 1), "93": (0, 1), "96": (0, 2), "85": (0, 1), "90": (0, 1), }

    select 5 Team combinations


    • COMBINATION 3--Alabama--89, Trenton:--93,--Dublin--96, Chicago: 96,--Seattle--90


    • Remaining Places and their associated points SET A --Trenton--96, --Trenton--90,--Trenton--85, SET A--Rutgers--93,--Rutgers--96, --Rutgers--90,--Rutgers--85, SET B --Alabama--96, --Alabama--90,--Alabama--85, SET B --Baylor--96,--Baylor--89,--Baylor--90,--Baylor--85, SET C--Orlando--93, --Orlando--89,--Orlando--90,--Orlando--85, SET C--Miami--93, --Miami--89,--Miami--90,--Miami--85, SET D--Atlanta--93, --Atlanta--89,--Atlanta--90,
      SET D--Dublin--93, --Dublin--89,--Dublin--90,--Dublin--85, SET E--Chicago--93, --Chicago--89, --Chicago--85, SET E--SFO--93,--SFO--96,--SFO--89, --SFO--85, SET J--Seattle--93,--Seattle--96,--Seattle--89, --Seattle--85, SET J--Spokane--93,--Spokane--96,--Spokane--89,--Spokane--90,--Spokane--85,

      From here again as per constraints point_constraints = { "89": (0, 1), "93": (0, 1), "96": (0, 2), "85": (0, 1), "90": (0, 1), }

    select 5 Team combinations


    • COMBINATION 4-- --Baylor--89,--Rutgers--93,--SFO--96,--Seattle--96--Trenton--90,


    • Remaining Places and their associated points SET A --Trenton--96, --Trenton--85, SET A--Rutgers--96, --Rutgers--90,--Rutgers--85, SET B --Alabama--96, --Alabama--90,--Alabama--85, SET B --Baylor--96, --Baylor--90,--Baylor--85, SET C--Orlando--93, --Orlando--89,--Orlando--90,--Orlando--85, SET C--Miami--93, --Miami--89,--Miami--90,--Miami--85, SET D--Atlanta--93, --Atlanta--89,--Atlanta--90,
      SET D--Dublin--93, --Dublin--89,--Dublin--90,--Dublin--85, SET E--Chicago--93, --Chicago--89, --Chicago--85, SET E--SFO--93--SFO--89, --SFO--85, SET J--Seattle--93,--Seattle--89, --Seattle--85, SET J--Spokane--93,--Spokane--96,--Spokane--89,--Spokane--90,--Spokane--85,

    --

    From here again as per constraints point_constraints = {
    "89": (0, 1),
    "93": (0, 1),
    "96": (0, 2),
    "85": (0, 1),
    "90": (0, 1),
    }


    select 5 Team combinations


    • COMBINATION 5 --Orlando--89, Atlanta--93, --Spokane--96, --Trenton--96, --Alabama--85,


    • Remaining Places and their associated points

    SET A --Trenton--85, SET A--Rutgers--96, --Rutgers--90,--Rutgers--85, SET B --Alabama--96, --Alabama--90, SET B --Baylor--96, --Baylor--90,--Baylor--85, SET C--Orlando--93, --Orlando--90,--Orlando--85, SET C--Miami--93, --Miami--89,--Miami--90,--Miami--85, SET D-- --Atlanta--89,--Atlanta--90,
    SET D--Dublin--93, --Dublin--89,--Dublin--90,--Dublin--85, SET E--Chicago--93, --Chicago--89, --Chicago--85, SET E--SFO--93--SFO--89, --SFO--85, SET J--Seattle--93,--Seattle--89, --Seattle--85, SET J--Spokane--93, --Spokane--89,--Spokane--90,--Spokane--85,

    And so on

    • COMBINATION 1- Trenton: 89,--Alabama--93, --Orlando--96, --Atlanta--96, --Chicago--90
    • COMBINATION 2- Rutgers: 89,--Baylor--93, --Miami--96,--Atlanta--85,--SFO--90,
    • COMBINATION 3--Alabama--89, Trenton:--93,--Dublin--96, Chicago: 96,--Seattle--90
    • COMBINATION 4-- --Baylor--89,--Rutgers--93,--SFO--96,--Seattle--96--Trenton--90,
    • COMBINATION 5 --Orlando--89, Atlanta--93,--Spokane--96,--Trenton--96,--Alabama--85,

    Continue reading...

Compartilhe esta Página