Skip to content

Commit 6673a67

Browse files
committed
feat(algorithms, arrays): tournament winner
Tournament winner algorithm to determine wthe winner in a tournament
1 parent 3a802dd commit 6673a67

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

algorithms/arrays/sorted_squared_array/test_sorted_squared_array.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ def test_2(self):
1717
actual = sorted_squared_array(input_array)
1818
self.assertEqual(expected, actual)
1919

20+
def test_3(self):
21+
"""for an input of [-7,-3,2,3,11] it should return [4,9,9,49,121]"""
22+
input_array = [-7,-3,2,3,11]
23+
expected = [4,9,9,49,121]
24+
actual = sorted_squared_array(input_array)
25+
self.assertEqual(expected, actual)
26+
2027

2128
class SortedSquaredArray2TestCases(unittest.TestCase):
2229
def test_1(self):
@@ -33,6 +40,13 @@ def test_2(self):
3340
actual = sorted_squared_array_2(input_array)
3441
self.assertEqual(expected, actual)
3542

43+
def test_3(self):
44+
"""for an input of [-7,-3,2,3,11] it should return [4,9,9,49,121]"""
45+
input_array = [-7,-3,2,3,11]
46+
expected = [4,9,9,49,121]
47+
actual = sorted_squared_array_2(input_array)
48+
self.assertEqual(expected, actual)
49+
3650

3751
if __name__ == '__main__':
3852
unittest.main()
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Tournament Winner
2+
3+
There's an algorithms tournament taking place in which teams of programmers
4+
compete against each other to solve algorithmic problems as fast as possible.
5+
Teams compete in a round robin, where each team faces off against all other
6+
teams. Only two teams compete against each other at a time, and for each
7+
competition, one team is designated the home team, while the other team is the
8+
away team. In each competition there's always one winner and one loser; there
9+
are no ties. A team receives 3 points if it wins and 0 points if it loses. The
10+
winner of the tournament is the team that receives the most amount of points.
11+
12+
Given an array of pairs representing the teams that have competed against each
13+
other and an array containing the results of each competition, write a
14+
function that returns the winner of the tournament. The input arrays are named
15+
`competitions` and `results`, respectively. The
16+
`competitions` array has elements in the form of
17+
`[homeTeam, awayTeam]`, where each team is a string of at most 30
18+
characters representing the name of the team. The `results` array
19+
contains information about the winner of each corresponding competition in the
20+
`competitions` array. Specifically, `results[i]` denotes
21+
the winner of `competitions[i]`, where a <span>1</span> in the
22+
`results` array means that the home team in the corresponding
23+
competition won and a `0` means that the away team won.
24+
25+
It's guaranteed that exactly one team will win the tournament and that each
26+
team will compete against all other teams exactly once. It's also guaranteed
27+
that the tournament will always have at least two teams.
28+
29+
```plain
30+
Sample Input
31+
32+
competitions = [["HTML", "C#"], ["C#", "Python"], ["Python", "HTML"]]
33+
results = [0, 0, 1]
34+
35+
Sample Output
36+
"Python"
37+
38+
Explanation
39+
C# beats HTML, Python beats C#, and Python beats HTML.
40+
HTLM - 0 points
41+
C# - 3 points
42+
Python - 6 points
43+
That means that Python is the tournament winner and we return "Python".
44+
```
45+
46+
## Hints
47+
48+
1. Don't overcomplicate this problem. How would you solve it by hand? Consider that approach and try to translate it
49+
into code
50+
2. Use a hash table to store the total points collected by each team, with the team names as the keys in the hash table.
51+
Once you know how many points each team has, how can you determine which one is the winner?
52+
3. Loop through all of the competitions, and update the hash table at every
53+
iteration. For each competition, consider the name of the winning team; if the
54+
name already exists in the hash table, update that entry by adding 3 points to
55+
it. If the team name doesn't exist in the hash table, add a new entry in the
56+
hash table with the key as the team name and the value as 3 (since the team
57+
won its first competition). While looping through all of the competitions,
58+
keep track of the team with the highest score, and at the end of the
59+
algorithm, return the team with the highest score.
60+
61+
## Optimal Space & Time Complexity
62+
63+
O(n) time | O(k) space - where n is the number of competitions and k is the number of teams
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import List, Dict
2+
3+
4+
def tournament_winner(competitions: List[List[str]], results: List[int]) -> str:
5+
# We need to store a dictionary with the name of the team as the key and the number of points as the value
6+
# 3 points for a win, 0 points for a loss
7+
winner = ""
8+
teams: Dict[str, int] = {}
9+
for competition, result in zip(competitions, results):
10+
# result of 0 means that the away team won
11+
# add the teams to the dictionary with the number of points
12+
home_team = competition[0]
13+
away_team = competition[1]
14+
15+
teams[home_team] = teams.get(home_team, 0)
16+
teams[away_team] = teams.get(away_team, 0)
17+
18+
if result == 0:
19+
teams[away_team] = teams.get(away_team, 0) + 3
20+
else:
21+
teams[home_team] = teams.get(home_team, 0) + 3
22+
23+
# find the team with the most points
24+
for team, points in teams.items():
25+
if points > teams.get(winner, 0):
26+
winner = team
27+
28+
return winner
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import unittest
2+
from . import tournament_winner
3+
4+
class TournamentWinnerTestCase(unittest.TestCase):
5+
def test_1(self):
6+
competitions = [["HTML", "C#"], ["C#", "Python"], ["Python", "HTML"]]
7+
results = [0, 0, 1]
8+
expected = "Python"
9+
actual = tournament_winner(competitions, results)
10+
self.assertEqual(expected, actual)
11+
12+
def test_2(self):
13+
competitions = [["HTML", "Java"],["Java", "Python"],["Python", "HTML"]]
14+
results = [0, 1, 1]
15+
expected = "Java"
16+
actual = tournament_winner(competitions, results)
17+
self.assertEqual(expected, actual)
18+
19+
20+
if __name__ == '__main__':
21+
unittest.main()

0 commit comments

Comments
 (0)