[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky]
[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Display team names for a round robin tournament in Python

[A round robin schedule]

The example Generate a schedule for a round robin tournament in Python explains how you can generate a schedule for a round robin tournament. This example simply adds the ability to display the schedule using team names instead of team numbers.

The following generate_round_robin_names method creates a schedule that contains team names.

def generate_round_robin_names(team_names): # Generate the schedule. num_teams = len(team_names) schedule = generate_round_robin(num_teams) # Replace team numbers with names. name_schedule = schedule.copy() num_rounds = len(schedule[0]) for team in range(num_teams): for round in range(num_rounds): if name_schedule[team][round] != 'bye': name_schedule[team][round] = team_names[name_schedule[team][round]] # Insert this team at the beginning of its list. name_schedule[team].insert(0, team_names[team]) return name_schedule

This method calls the previous post's generate_round_robin method to make a schedule for the required number of teams. It then loops through the schedule replacing team numbers with team names. It also adds each team's name to the start of the list that gives that team's schedule.

The following code shows the revised print_schedule method that displays the new schedule format.

def print_schedule(schedule): num_teams = len(schedule) num_rounds = len(schedule[0]) for round in range(1, num_rounds): print(f'Round {round}') for team1_number in range(num_teams): team1 = schedule[team1_number][0] team2 = schedule[team1_number][round] # Only print if it's a bye or team 1 < team 2. if team2 == 'bye': print(f' {team1} has a bye') elif team1 < team2: print(f' {team1} vs. {team2}')

The only real difference between this version and the one used by the previous post is that this one gets the teams' names from the schedule.

Download the example to see all of the details.

© 2024 Rocky Mountain Computer Consulting, Inc. All rights reserved.