diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2025-10-14 09:44:44 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2025-10-14 09:44:44 +0200 |
| commit | ca867fd5130d8772feb8d95e18bf6a801395f245 (patch) | |
| tree | b76eda186f17cec5c34112d03db95472ca728201 | |
| parent | 4781aa70debbc13c1be52b833bfb6ccb8a9b16c3 (diff) | |
| download | perlweeklychallenge-club-ca867fd5130d8772feb8d95e18bf6a801395f245.tar.gz perlweeklychallenge-club-ca867fd5130d8772feb8d95e18bf6a801395f245.tar.bz2 perlweeklychallenge-club-ca867fd5130d8772feb8d95e18bf6a801395f245.zip | |
Add Python solution for challenge 343 task 2
| -rw-r--r-- | challenge-343/lubos-kolouch/python/ch-2.py | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/challenge-343/lubos-kolouch/python/ch-2.py b/challenge-343/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..52f208ce2d --- /dev/null +++ b/challenge-343/lubos-kolouch/python/ch-2.py @@ -0,0 +1,86 @@ +#!/usr/bin/env python3 +""" +Perl Weekly Challenge: Task 2 - Champion Team +Find the team with the most wins, or the strongest among those with the most wins. +""" + +import unittest + + +def champion_team(grid: list[list[int]]) -> str: + """ + Find the champion team with the most wins, or the strongest among those tied. + + Args: + grid (list[list[int]]): Matrix where grid[i][j] == 1 means team i beats team j, + grid[i][j] == 0 means team j beats team i. + + Returns: + str: The champion team as "Team X" where X is the team index. + """ + n = len(grid) + if n == 0: + return "Team -1" + + # Count wins for each team + wins = [sum(grid[i][j] for j in range(n) if i != j) for i in range(n)] + + # Find maximum wins + max_wins = max(wins) + + # Find teams with maximum wins + top_teams = [i for i, win in enumerate(wins) if win == max_wins] + + # If only one team has max wins, return it + if len(top_teams) == 1: + return f"Team {top_teams[0]}" + + # Among top teams, find the one that beats others + for i in top_teams: + is_champion = True + for j in top_teams: + if i != j and not grid[i][j]: + is_champion = False + break + if is_champion: + return f"Team {i}" + + # Fallback (should not occur given problem constraints) + return f"Team {top_teams[0]}" + + +# Unit tests + + +class TestChampionTeam(unittest.TestCase): + """Unit tests for the champion_team function.""" + + def test_case_1(self): + """Test Case 1: 3x3 grid -> Team 0""" + grid = [[0, 1, 1], [0, 0, 1], [0, 0, 0]] + self.assertEqual(champion_team(grid), 'Team 0') + + def test_case_2(self): + """Test Case 2: 4x4 grid -> Team 3""" + grid = [[0, 1, 0, 0], [0, 0, 0, 0], [1, 1, 0, 0], [1, 1, 1, 0]] + self.assertEqual(champion_team(grid), 'Team 3') + + def test_case_3(self): + """Test Case 3: 4x4 grid -> Team 0""" + grid = [[0, 1, 0, 1], [0, 0, 1, 1], [1, 0, 0, 0], [0, 0, 1, 0]] + self.assertEqual(champion_team(grid), 'Team 0') + + def test_case_4(self): + """Test Case 4: 3x3 grid -> Team 0""" + grid = [[0, 1, 1], [0, 0, 0], [0, 1, 0]] + self.assertEqual(champion_team(grid), 'Team 0') + + def test_case_5(self): + """Test Case 5: 5x5 grid -> Team 2""" + grid = [[0, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 1, 0, 1, 1], + [1, 1, 0, 0, 0], [1, 1, 0, 1, 0]] + self.assertEqual(champion_team(grid), 'Team 2') + + +if __name__ == '__main__': + unittest.main() |
