← 返回 reddit 的题目列表Design a Tennis Match Scoring System
类型:online_judge
Problem: Design a Tennis Scoring System
Design an object-oriented tennis scoring system. First implement a single game TennisGame, then extend it to a set TennisSet.
Part 1: Single Game TennisGame
Implement:
class TennisGame:
def __init__(self, player1: str, player2: str):
pass
def add_score(self, player: str) -> None:
"""Record that the given player won one point."""
pass
def get_score(self) -> tuple[int, int]:
"""Return raw points, e.g. (3, 2)."""
pass
def get_result(self) -> str:
"""Return the winner name if the game is over; otherwise return an empty string."""
pass
def get_human_score(self) -> str:
"""Return a tennis-style score such as Love-15, Deuce, Advantage A, or Game A."""
pass
Game rules
Score name mapping:
0 -> Love
1 -> 15
2 -> 30
3 -> 40
A player wins the game only if they have at least 4 points and lead by at least 2 points.
3-3 is Deuce.
If a player wins a point from Deuce, they get Advantage <player>.
If the player with advantage wins the next point, they win the game.
If the player without advantage wins the next point, the score resets to 3-3, i.e. Deuce.
To prevent unbounded internal scores, whenever both players have equal scores and both scores are at least 3, reset the internal raw score to 3-3.
Once a game is over, further calls to add_score should raise an error or reject the update.
Invalid player names should raise an error.
Part 2: Set Scoring TennisSet
Implement:
class TennisSet:
def __init__(self, player1: str, player2: str, games_to_win: int = 3):
pass
def add_score(self, player: str) -> None:
"""Record one point in the current game."""
pass
def get_set_score(self) -> tuple[int, int]:
"""Return the number of games won by each player in the current set."""
pass
def get_set_winner(self) -> str:
"""Return the set winner if the set is over; otherwise return an empty string."""
pass
Set rules
A set consists of multiple games.
The first player to win games_to_win games wins the set. For example, games_to_win = 3 means first to 3 games wins.
After each game ends, if the set is not over, create a fresh TennisGame; the game score resets to 0-0.
Once the set is over, further scoring should raise an error or reject the update.
Part 3: Side Switching
Extend TennisSet:
Initially, player1 is on near, and player2 is on far.
After every odd-numbered completed game, players switch sides, e.g. after games 1, 3, 5, etc.
Side switching is presentation-only and must not affect scoring logic.
Provide:
def get_sides(self) -> dict[str, str]:
"""Return {player_name: 'near' | 'far'}."""
Input/output format for automated testing
In an interview, the main task is to implement the classes. For automated testing, use the following command format.
First line:
player1 player2 games_to_win
Second line:
Q
Next Q lines contain commands:
POINT player: player wins one point in the current game. If an error occurs, print ERROR: <message>.
GAME_SCORE: print the current raw game score, e.g. 3-3.
GAME_HUMAN: print the current human-readable game score.
GAME_RESULT: print the current game winner, or an empty line if none.
SET_SCORE: print the set score, e.g. 2-1.
SET_WINNER: print the set winner, or an empty line if none.
SIDES: print side assignments, e.g. A:near B:far.
Constraints
Player names contain no spaces, and the two names are distinct.
1 <= Q <= 100000.
Every operation should run in O(1) time.
Example
Input
A B 3
6
POINT A
POINT A
POINT A
GAME_HUMAN
POINT A
SET_SCORE
Output
40-Love
1-0