From 127ca5556cce26c1a069f95af1908995b7562fa5 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Sun, 26 May 2024 22:30:59 +1000 Subject: sgreen solutions to challenge 270 --- challenge-270/sgreen/python/ch-1.py | 43 ++++++++++++++++++++++++++++++++ challenge-270/sgreen/python/ch-2.py | 49 +++++++++++++++++++++++++++++++++++++ challenge-270/sgreen/python/test.py | 23 +++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100755 challenge-270/sgreen/python/ch-1.py create mode 100755 challenge-270/sgreen/python/ch-2.py create mode 100755 challenge-270/sgreen/python/test.py (limited to 'challenge-270/sgreen/python') diff --git a/challenge-270/sgreen/python/ch-1.py b/challenge-270/sgreen/python/ch-1.py new file mode 100755 index 0000000000..3cfe7dbe3e --- /dev/null +++ b/challenge-270/sgreen/python/ch-1.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +import json +import sys + + +def special_positions(matrix: list) -> int: + rows = len(matrix) + cols = len(matrix[0]) + special_position = 0 + + # Count the number of ones in each row and column + row_count = [0] * rows + col_count = [0] * cols + + for row in range(rows): + # Check this has the same number of columns as the first row + if len(matrix[row]) != cols: + raise ValueError("Row %s has the wrong number of columns", row) + + for col in range(cols): + if matrix[row][col]: + row_count[row] += 1 + col_count[col] += 1 + + # Find the number of special positions. This is true if the value is one + # and the row_count and col_count is one + for row in range(rows): + for col in range(cols): + if matrix[row][col] and row_count[row] == 1 and col_count[col] == 1: + special_position += 1 + + return special_position + + +def main(): + matrix = json.loads(sys.argv[1]) + result = special_positions(matrix) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-270/sgreen/python/ch-2.py b/challenge-270/sgreen/python/ch-2.py new file mode 100755 index 0000000000..d285799375 --- /dev/null +++ b/challenge-270/sgreen/python/ch-2.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 + +import sys + + +def equalize_array(ints: list, x: int, y: int) -> str: + score = 0 + # Calculate the needed values + max_value = max(ints) + needed = [max_value - i for i in ints] + + # If we have at least two items, and y is less than 2 × x, lets remove two + # values at a time while we can + if len(ints) > 1 and y < x * 2: + while True: + # Sort the positions by the values they hold + sorted_index = sorted( + range(len(ints)), + key=lambda index: needed[index], + reverse=True + ) + + # If we don't have two non zero values, we are done with level 2 + if needed[sorted_index[1]] == 0: + break + + # Take one off each number at that position + needed[sorted_index[0]] -= 1 + needed[sorted_index[1]] -= 1 + score += y + + # As level one takes one off each number, we simply multiple the remaining + # needed values by the x value + score += sum(needed) * x + + return score + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + y = array.pop() + x = array.pop() + result = equalize_array(array, x, y) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-270/sgreen/python/test.py b/challenge-270/sgreen/python/test.py new file mode 100755 index 0000000000..5e279ab74f --- /dev/null +++ b/challenge-270/sgreen/python/test.py @@ -0,0 +1,23 @@ +#!/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.special_positions([[1, 0, 0], [0, 0, 1], [1, 0, 0]]), 1 + ) + self.assertEqual( + ch_1.special_positions([[1, 0, 0], [0, 1, 0], [0, 0, 1]]), 3 + ) + + def test_ch_2(self): + self.assertEqual(ch_2.equalize_array([4, 1], 3, 2), 9) + self.assertEqual(ch_2.equalize_array([2, 3, 3, 3, 5], 2, 1), 6) + + +if __name__ == '__main__': + unittest.main() -- cgit