diff options
Diffstat (limited to 'challenge-337/sgreen/python')
| -rwxr-xr-x | challenge-337/sgreen/python/ch-1.py | 34 | ||||
| -rwxr-xr-x | challenge-337/sgreen/python/ch-2.py | 63 | ||||
| -rwxr-xr-x | challenge-337/sgreen/python/test.py | 25 |
3 files changed, 122 insertions, 0 deletions
diff --git a/challenge-337/sgreen/python/ch-1.py b/challenge-337/sgreen/python/ch-1.py new file mode 100755 index 0000000000..565e8bfd00 --- /dev/null +++ b/challenge-337/sgreen/python/ch-1.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +import sys + + +def smaller_than_current(numbers: list) -> list: + """For each number in the list, count how many numbers are smaller than it. + + Args: + numbers: List of numbers. + + Returns: + A list of counts, where each count corresponds to how many numbers in the + input list are smaller than or equal to the number at that index. + """ + solution = [] + + for number in numbers: + # Count how many numbers are smaller or equal than the current number. + # Subtract one to exclude the current number itself. + solution.append(sum(1 for n in numbers if n <= number) - 1) + + return solution + + +def main(): + # Convert input into numbers + array = [float(n) for n in sys.argv[1:]] + result = smaller_than_current(array) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-337/sgreen/python/ch-2.py b/challenge-337/sgreen/python/ch-2.py new file mode 100755 index 0000000000..a6fb4e597c --- /dev/null +++ b/challenge-337/sgreen/python/ch-2.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 + +from dataclasses import dataclass +import sys + + +@dataclass +class Point: + """A point in the matrix.""" + x: int + y: int + + def score(self, row:int, col:int) -> int: + """Calculate the score for this point at the given row and column.""" + if row == self.x and col == self.y: + return 2 + if row == self.x or col == self.y: + return 1 + + return 0 + +def odd_matrix(rows:int, cols:int, points_list: list) -> int: + """Calculate the number of cells in the matrix with an odd score. + + Args: + rows: Number of rows in the matrix. + col: Number of columns in the matrix. + points_list: List of (x, y) tuples representing points in the matrix. + + Returns: + The number of cells in the matrix with an odd score. + """ + # Convert the list of points into Point objects + points = [Point(x, y) for x, y in points_list] + odd_cells = 0 + + for row in range(rows): + for col in range(cols): + # Calculate the score for this cell + score = 0 + for point in points: + score += point.score(row, col) + + # If the score is odd, increment the count of odd cells + if score % 2 == 1: + odd_cells += 1 + + return odd_cells + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + row = array.pop(0) + col = array.pop(0) + points = [(array[i], array[i + 1]) for i in range(0, len(array), 2)] + + result = odd_matrix(row, col, points) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-337/sgreen/python/test.py b/challenge-337/sgreen/python/test.py new file mode 100755 index 0000000000..8223307e20 --- /dev/null +++ b/challenge-337/sgreen/python/test.py @@ -0,0 +1,25 @@ +#!/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.smaller_than_current([6, 5, 4, 8]), [2, 1, 0, 3]) + self.assertEqual(ch_1.smaller_than_current([7, 7, 7, 7]), [3, 3, 3, 3]) + self.assertEqual(ch_1.smaller_than_current([5, 4, 3, 2, 1]), [4, 3, 2, 1, 0]) + self.assertEqual(ch_1.smaller_than_current([-1, 0, 3, -2, 1]), [1, 2, 4, 0, 3]) + self.assertEqual(ch_1.smaller_than_current([0, 1, 1, 2, 0]), [1, 3, 3, 4, 1]) + + def test_ch_2(self): + self.assertEqual(ch_2.odd_matrix(2, 3, [[0,1],[1,1]]), 6) + self.assertEqual(ch_2.odd_matrix(2, 2, [[1,1],[0,0]]), 0) + self.assertEqual(ch_2.odd_matrix(3, 3, [[0,0],[1,2],[2,1]]), 0) + self.assertEqual(ch_2.odd_matrix(1, 5, [[0,2],[0,4]]), 2) + self.assertEqual(ch_2.odd_matrix(4, 2, [[1,0],[3,1],[2,0],[0,1]]), 8) + + +if __name__ == '__main__': + unittest.main() |
