The tennis calculator takes a set of scores as inputs and produces useful statistics based on those scores.
This calculator will used a simplified version of scoring where whoever gets to 6 games first wins the set
The solution for the Tennis calculator is implemented in Typescript + Node.js application. For the unit test cases, Jest has been used.
Install Node.js version 20. Download from https://nodejs.org/en/download/. After install verify using --version if the node version is 20.
cd <project-name>
npm install
-
To get result of both the queries (match result query and games for a particular player), run the below command
npm start 'fullTournament.txt' 'Score Match 01' 'Games Player Person A'
-
To get result of only match result query, run the below commands
npm start 'fullTournament.txt' 'Score Match 01'
or
npm start 'fullTournament.txt' 'Score Match 02'
-
To get result of only games for a particular player, run the below commands
npm start 'fullTournament.txt' '' 'Games Player Person A'
or
npm start 'fullTournament.txt' '' 'Games Player Person B'
or
npm start 'fullTournament.txt' '' 'Games Player Person C'
-
Run the unit test cases.
npm run test
-
Run the application in an IDE(preferably visual studio code) and reload it every time after a change is made in code, execute the below command.
npm run dev
Note: More test data scenarios has been covered in the unit test cases
The Tennis Calculator takes inputs in the form of a list of points of a tennis match.
Given this list of points, it will calculate the "games", "sets" and "matches" results.
From there it can be queried about various statistics around the input matches it received.
The input will have some header lines, and then a list of points. For example:, the following would result in 2 games to "Person A":
Match: 01
Person A vs Person B
0
1
0
1
0
0
0
0
0
0
The first row is a match id, the second row shows who is playing against whom. After that are a series of points, where 0 is a point for the first person listed, 1 is for last person.
i.e.
Input | Score |
---|---|
Match: 01 | |
Person A vs Person B | |
0 | 15 - 0 |
1 | 15 - 15 |
0 | 30 - 15 |
1 | 30 - 30 |
0 | 40 - 30 |
0 | Game |
0 | 15 - 0 |
0 | 30 - 0 |
0 | 40 - 0 |
0 | Game |
For processing, blank lines must be ignored
Query scores for a particular match Prints who defeated whom, and the result of the sets for the match (winning player score first).
Query: Score Match <id>
Example: Score Match 01
Example output:
Person A defeated Person B
2 sets to 0
Prints a summary of games won vs lost for a particular player over the tournament
Query: Games Player <Player Name>
Example: Games Player Person A
Example output:
23 17
Running the application against the 'full_tournament.txt' file results in the following:
$ python tennis_calculator_app.py test/test_data/full_tournament.txt << EOF
Score Match 02
Games Player Person A
EOF
Person C defeated Person A
2 sets to 1
23 17
Details of tennis scoring can be found online. See here for reference:
https://en.wikipedia.org/wiki/Tennis_scoring_system
The variation used for this application is a best of 3 sets match, with first to 6 games wins a set.
Details as follows:
- A tennis match is split up into points, games and sets.
- Winning a game requires a person to win 4 points, but they must be ahead by at least 2 points (deuce, advantage, game)
- The first player to win 6 games wins a set. I.e:
- Players do NOT need to be ahead by 2 to win a set (6-5 finishes a set)
- There is nothing special about that final game in a set. All games are the same.
- Best of 3 sets (first to 2 sets wins).