Rally Scoreboard
MediumOnline Assessment
Turn a rally-by-rally log of a table-tennis match into the game scores and the current status, including who serves next.
Two players, A and B, play a match. A game is won by the first player to reach target points and lead by at least 2, so at target - 1 each the game continues until someone is two ahead (for example, with target = 11, the scores 11-9 and 14-12 end a game, but 11-10 does not). target is always odd.
A match is the best of games games (games is odd): the first player to win games // 2 + 1 games wins the match. Once the match is won, any remaining rallies in the log are ignored.
Serving. The player who serves first in game 1 is given. The first server alternates every game: the opposite player serves first in game 2, and so on. Within a game, the serve changes hands every 2 rallies (the first server serves rallies 1 and 2, the other player rallies 3 and 4, and so on). Once both players have at least target - 1 points, the serve changes after every rally instead, continuing from whoever is next to serve.
Input: target; games; first_server, "A" or "B"; rallies, a string of "A" and "B" letters, the winner of each rally in order.
Output: a list of strings. First, one string "x-y" for each finished game in order, where x is A's points and y is B's. Then one final status string:
- if the match has been won:
"MATCH <winner> <games won by the winner>-<games won by the loser>", for example"MATCH A 3-1"; - otherwise:
"LIVE <A's games>-<B's games> <A's points>-<B's points> <next server>"describing the unfinished game and who serves the next rally. If a game has just finished, the unfinished game is the next one, at0-0, with its first server as next server.
Example 1
- Input:
- [11,3,"A","AAAAAAAAAAABBBB"]
- Output:
- ["11-0","LIVE 1-0 0-4 B"]
- Explanation
- Target 11, best of 3, A serves first. A wins the first 11 rallies, so game 1 ends 11-0 and B serves first in game 2. In game 2 the four rallies
BBBBgive B four points. B served rallies 1 and 2, A served rallies 3 and 4, so B serves rally 5:LIVE 1-0 0-4 B.
Example 2
- Input:
- [3,1,"B","ABABAB"]
- Output:
- ["LIVE 0-0 3-3 B"]
- Explanation
- Target 3, best of 1, B serves first, log
ABABAB. The score runs 1-0, 1-1, 2-1, 2-2. Rallies 1-2 were served by B and 3-4 by A. At 2-2 both players havetarget - 1 = 2points, so the serve now changes after every rally: B serves rally 5 (3-2: nobody has won, the lead is only 1), A serves rally 6 (3-3), so B serves the next one. OutputLIVE 0-0 3-3 B.
Example 3
- Input:
- [5,3,"A","AAABBBAABBBBBAAAAABA"]
- Output:
- ["5-3","0-5","5-0","MATCH A 2-1"]
- Explanation
- Target 5, best of 3, A serves first. Game 1 ends 5-3 for A (
AAABBBAA). Game 2 is first served by B and ends 5-0 to B. Game 3 is served first by A and ends 5-0 to A, which gives A the match 2-1:MATCH A 2-1. The finalBAin the log is ignored because the match is already decided.
Constraints
- target is an odd integer between 3 and 21
- games is an odd integer between 1 and 7
- first_server is "A" or "B"
- 0 ≤ rallies.length ≤ 400; rallies contains only the characters A and B