aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-343/sgreen/README.md4
-rw-r--r--challenge-343/sgreen/blog.txt1
-rwxr-xr-xchallenge-343/sgreen/python/ch-1.py26
-rwxr-xr-xchallenge-343/sgreen/python/ch-2.py71
-rwxr-xr-xchallenge-343/sgreen/python/test.py76
5 files changed, 176 insertions, 2 deletions
diff --git a/challenge-343/sgreen/README.md b/challenge-343/sgreen/README.md
index 33d3a4920d..dd69eee606 100644
--- a/challenge-343/sgreen/README.md
+++ b/challenge-343/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 342
+# The Weekly Challenge 343
-Blog: [Balancing the Score](https://dev.to/simongreennet/weekly-challenge-balancing-the-score-38kd)
+Blog: [Absolute Champion](https://dev.to/simongreennet/weekly-challenge-absolute-champion-57cf)
diff --git a/challenge-343/sgreen/blog.txt b/challenge-343/sgreen/blog.txt
new file mode 100644
index 0000000000..f3d4d3fb08
--- /dev/null
+++ b/challenge-343/sgreen/blog.txt
@@ -0,0 +1 @@
+https://dev.to/simongreennet/weekly-challenge-absolute-champion-57cf \ No newline at end of file
diff --git a/challenge-343/sgreen/python/ch-1.py b/challenge-343/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..97295cab61
--- /dev/null
+++ b/challenge-343/sgreen/python/ch-1.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python3
+
+from decimal import Decimal
+import sys
+
+
+def zero_friend(numbers: list) -> Decimal:
+ """
+ Return the number closest to zero from the list.
+
+ Input: A list of numbers
+
+ Returns: The number closest to zero
+ """
+ return min(abs(n) for n in numbers)
+
+
+def main():
+ # Convert input into numbers
+ array = [Decimal(n) for n in sys.argv[1:]]
+ result = zero_friend(array)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-343/sgreen/python/ch-2.py b/challenge-343/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..aa0a409eb5
--- /dev/null
+++ b/challenge-343/sgreen/python/ch-2.py
@@ -0,0 +1,71 @@
+#!/usr/bin/env python3
+
+import json
+import re
+import sys
+
+
+def champion_team(matrix: list[list[int]], consider_teams: list[int]|None = None) -> str:
+ """
+ Determine the champion team from a results matrix.
+
+ Input: A square matrix of 0s and 1s representing the strengths of teams.
+
+ Returns: The champion team as a string, or "No champion" if there is a tie.
+ """
+ # Check the matrix is a square
+ size = len(matrix)
+ if any(len(row) != size for row in matrix):
+ raise ValueError("Matrix must be square")
+
+ # Check the matrix only contains only 0s and 1s
+ if any(matrix[i][j] not in (0, 1) for i in range(size) for j in range(size)):
+ raise ValueError("Matrix must only contain 0s and 1s")
+
+ # Check a team does not win against itself
+ if any(matrix[i][i] != 0 for i in range(size)):
+ raise ValueError("A team cannot win against itself")
+
+ # Consider all teams by default
+ if consider_teams is None:
+ consider_teams = list(range(len(matrix)))
+
+ max_wins = -1
+ winning_teams = []
+
+ for team_index, results in enumerate(matrix):
+ # Skip teams we don't need to look at
+ if team_index not in consider_teams:
+ continue
+
+ # Find the wins for this team against other teams being considered
+ wins = sum(results[i] for i in consider_teams)
+
+ # If it is the best so far, update our records
+ if wins > max_wins:
+ max_wins = wins
+ winning_teams = [team_index]
+ elif wins == max_wins:
+ winning_teams.append(team_index)
+
+ if len(winning_teams) == 1:
+ # We have one winner
+ return f"Team {winning_teams[0]}"
+
+ if len(winning_teams) == len(consider_teams):
+ # There is a tie between two or more teams
+ return "No champion"
+
+ # Call the function recursively to break the tie
+ return champion_team(matrix, consider_teams=winning_teams)
+
+
+def main():
+ # Convert input into a list of lists
+ matrix = json.loads(sys.argv[1])
+ result = champion_team(matrix)
+ print(result)
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-343/sgreen/python/test.py b/challenge-343/sgreen/python/test.py
new file mode 100755
index 0000000000..0b0846e4e0
--- /dev/null
+++ b/challenge-343/sgreen/python/test.py
@@ -0,0 +1,76 @@
+#!/usr/bin/env python3
+
+import unittest
+ch_1 = __import__('ch-1')
+ch_2 = __import__('ch-2')
+
+
+class TestClass(unittest.TestCase):
+ def test_ch_1(self):
+ self.assertEqual(ch_1.zero_friend([4, 2, -1, 3, -2]), 1)
+ self.assertEqual(ch_1.zero_friend([-5, 5, -3, 3, -1, 1]), 1)
+ self.assertEqual(ch_1.zero_friend([7, -3, 0, 2, -8]), 0)
+ self.assertEqual(ch_1.zero_friend([-2, -5, -1, -8]), 1)
+ self.assertEqual(ch_1.zero_friend([-2, 2, -4, 4, -1, 1]), 1)
+ self.assertEqual(ch_1.zero_friend([1.2, 2, 4, -5]), 1.2)
+
+ def test_ch_2(self):
+ matrix_1 = [
+ [0, 1, 1],
+ [0, 0, 1],
+ [0, 0, 0],
+ ]
+
+ matrix_2 = [
+ [0, 1, 0, 0],
+ [0, 0, 0, 0],
+ [1, 1, 0, 0],
+ [1, 1, 1, 0],
+ ]
+
+ matrix_3 = [
+ [0, 1, 0, 1],
+ [0, 0, 1, 1],
+ [1, 0, 0, 0],
+ [0, 0, 1, 0],
+ ]
+
+ matrix_4 = [
+ [0, 1, 1],
+ [0, 0, 0],
+ [0, 1, 0],
+ ]
+
+ matrix_5 = [
+ [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],
+ ]
+
+ matrix_6 = [
+ [0, 0, 1, 1],
+ [1, 0, 0, 1],
+ [0, 1, 0, 0],
+ [0, 0, 1, 0],
+ ]
+
+ matrix_7 = [
+ [0, 1, 0, 1],
+ [0, 0, 1, 1],
+ [1, 0, 0, 1],
+ [0, 0, 0, 0],
+ ]
+
+ self.assertEqual(ch_2.champion_team(matrix_1), 'Team 0')
+ self.assertEqual(ch_2.champion_team(matrix_2), 'Team 3')
+ self.assertEqual(ch_2.champion_team(matrix_3), 'Team 0')
+ self.assertEqual(ch_2.champion_team(matrix_4), 'Team 0')
+ self.assertEqual(ch_2.champion_team(matrix_5), 'Team 2')
+ self.assertEqual(ch_2.champion_team(matrix_6), 'Team 1')
+ self.assertEqual(ch_2.champion_team(matrix_7), 'No champion')
+
+
+if __name__ == '__main__':
+ unittest.main()