bball

py

School

University Of Arizona *

*We aren’t endorsed by this school

Course

120

Subject

Computer Science

Date

Apr 3, 2024

Type

py

Pages

4

Uploaded by MateClover22302

Report
""" File: bball.py Author: Nick Brobeck Course: CSC 120, Spring 2024 Purpose: This program defines classes to represent teams, conferences, and a set of conferences.It reads team information from a file, calculates the average win ratio for each conference, and determines the best conference(s) based on the highest average win ratio. """ class Team: # Represents a basketball team with its name, \ # conference, wins, and losses. def __init__(self, line): """Initialize a Team object with information parsed from a line. Parameters: line (str): A string containing team information in the format \ "name (conference) wins losses". Returns: None """ # Split the line by '(' to separate team name, conference, \ # and wins/losses. parts = line.strip().split('(') # Extract team name and conference. self.name = parts[0].strip() self.conf = parts[-1].split(')')[0].strip() wins, losses = [int(x) for x in parts[-1].split(')')[1].split()] self.wins = wins self.losses = losses def get_name(self): """Return the name of the team.""" return self.name def get_conf(self): """Return the conference of the team.""" return self.conf def win_ratio(self): """Calculate the win ratio of the team. Returns: float: The win ratio of the team. """ total_games = self.wins + self.losses return self.wins / total_games def __str__(self): """Return a string representation of the team.""" return "{} : {}".format(self.name, self.win_ratio()) class Conference: # Represents a basketball conference with \ # its name and a list of teams.
# Initialize the conference with its name. def __init__(self, conf): """Initialize a Conference object with a conference name. Parameters: conf (str): The name of the conference. Returns: None """ self.conf = conf # Initialize an empty list to store teams belonging to this conference. self.teams = [] def contains(self, team): """Check if a team is in the conference. Parameters: team (Team): The team to check. Returns: bool: True if the team is in the conference, False otherwise. """ return team in self.teams def get_name(self): """Return the name of the conference.""" return self.conf def add_team(self, team): """Add a team to the conference. Parameters: team (Team): The team to add. Returns: None """ self.teams.append(team) def win_ratio_avg(self): """Calculate the average win ratio of teams in the conference. Returns: float: The average win ratio of teams in the conference. """ total_win_ratio = sum(team.win_ratio() for team in self.teams) return total_win_ratio / len(self.teams) def __str__(self): """Return a string representation of the conference.""" return "{} : {}".format(self.conf, self.win_ratio_avg()) class ConferenceSet: # Represents a set of basketball conferences. def __init__(self): """Initialize a ConferenceSet object.
Returns: None """ self.conferences = {} def add_team(self, team): """Add a team to the appropriate conference. Parameters: team (Team): The team to add. Returns: None """ conf_name = team.get_conf() if conf_name not in self.conferences: self.conferences[conf_name] = Conference(conf_name) self.conferences[conf_name].add_team(team) def best_conferences(self): """Find the conference(s) with the highest average win ratio. Returns: list: A list of Conference objects representing the best conference(s). """ max_avg = 0 best_conferences = [] for conf in self.conferences.values(): avg = conf.win_ratio_avg() # Check if the current conference has a higher \ # average win ratio than the maximum. if avg > max_avg: max_avg = avg best_conferences = [conf] elif avg == max_avg: # If the current conference has the same average win ratio \ # as the maximum, add it to the list of best conferences. best_conferences.append(conf) # Sort the best conferences alphabetically by conference name. return sorted(best_conferences, key=Conference.get_name) def main(): filename = input() conference_set = ConferenceSet() with open(filename, 'r') as file: for line in file: if not line.startswith("#"): team = Team(line) conference_set.add_team(team) best_conferences = conference_set.best_conferences() for conf in best_conferences: print(conf)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
main()