Tennis Game Score
Problem statement
You are given two player names, playerOne and playerTwo, and a list winners. Each entry names the player who won the next point of one tennis game. Return the current game state after those points.
- A player wins after scoring at least
4points and leading by at least2. Return the player name followed byWINS. - If both players have at least
3points and their point counts are equal, returnDEUCE. - If both players have at least
3points and one leads by1, return the leading player name followed byADVANTAGE. - Otherwise, point counts
0,1,2, and3are displayed as0,15,30, and40. - For a
15-15tie, return15a. For a30-30tie, return30a. - For every other ordinary score, including
0-0, returnplayerOne scoreOne - playerTwo scoreTwo. Keep the names in parameter order.
Practice details
For this exercise, assume the points describe one valid game and end no later than its first winning point. Both names contain only English letters, are case-sensitive, are different, and have length from 1 to 20. The winner list contains from 0 to 10000 entries, each exactly one player name.
For this exercise, assume ordinary score lines use a single ASCII space on each side of the ASCII hyphen -. Other outputs contain exactly one space between the name and WINS or ADVANTAGE.
Function
tennisScore(playerOne: String, playerTwo: String, winners: String[]) → StringExamples
Example 1
playerOne = "Alex"playerTwo = "Blair"winners = ["Alex","Blair","Alex"]return = "Alex 30 - Blair 15"Alex wins 2 points and Blair wins 1, so their displayed scores are 30 and 15. The names stay in parameter order.
Example 2
playerOne = "Ada"playerTwo = "Ben"winners = ["Ada","Ben","Ada","Ben","Ada","Ben"]return = "DEUCE"Both players have 3 points, so the game is at deuce.
Example 3
playerOne = "Ivy"playerTwo = "Kai"winners = ["Ivy","Ivy","Ivy","Ivy"]return = "Ivy WINS"Ivy has 4 points and leads by 4, so Ivy wins.
Example 4
playerOne = "Mia"playerTwo = "Leo"winners = ["Mia","Leo","Mia","Leo","Mia","Leo","Mia"]return = "Mia ADVANTAGE"The first 6 points give a 3-3 tie. Mia then leads 4-3, which is advantage.
Constraints
- Each player name has length from
1to20. - Player names contain only English letters and are case-sensitive.
- The two player names are different.
- The winner list has length from
0to10000. - Every winner entry is exactly one of the two player names.
- The list describes one valid game and contains no point after the first winning point.