diff options
Diffstat (limited to 'challenge-257/sgreen/python')
| -rwxr-xr-x | challenge-257/sgreen/python/ch-1.py | 26 | ||||
| -rwxr-xr-x | challenge-257/sgreen/python/ch-2.py | 65 | ||||
| -rwxr-xr-x | challenge-257/sgreen/python/test.py | 26 |
3 files changed, 117 insertions, 0 deletions
diff --git a/challenge-257/sgreen/python/ch-1.py b/challenge-257/sgreen/python/ch-1.py new file mode 100755 index 0000000000..f2ca928579 --- /dev/null +++ b/challenge-257/sgreen/python/ch-1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +import sys + + +def smaller_than_current(ints: list) -> list: + """For each integer, how many integers are smaller than current one + + Args: + ints (list): The input list of integers + + Returns: + list: The output list of integers + """ + return [sum(1 for j in ints if j < i) for i in ints] + + +def main(): + # Convert input into integers + array = [int(n) for n in sys.argv[1:]] + result = smaller_than_current(array) + print('(' + ', '.join(map(str, result)) + ')') + + +if __name__ == '__main__': + main() diff --git a/challenge-257/sgreen/python/ch-2.py b/challenge-257/sgreen/python/ch-2.py new file mode 100755 index 0000000000..04eccc0243 --- /dev/null +++ b/challenge-257/sgreen/python/ch-2.py @@ -0,0 +1,65 @@ +#!/usr/bin/env python3 + +import json +import sys + + +def validate_matrix(matrix): + # Calculate the size of the matrix + rows = len(matrix) + cols = len(matrix[0]) + + for r in range(1, rows): + # Check that all columns are of equal length + if len(matrix[r]) != cols: + raise ValueError(f'Row {r} has different number of columns') + + +def echelon(matrix: list) -> int: + # Check that the matrix is valid + validate_matrix(matrix) + + # Get the position of the first one in each row + leading_one = [None if 1 not in row else row.index(1) for row in matrix] + + for row_num in range(len(matrix)): + row = matrix[row_num] + leading_one_pos = leading_one[row_num] + + # If the row is all zeros, there is nothing to check + if all(value == 0 for value in row): + continue + + # Check the first non zero number is one + for value in row: + if value == 1: + break + if value != 0: + return 0 + + # Check if the leading one is further right + if row_num != 0: + if leading_one[row_num - 1] is None: + # The previous row was all zeros. + return 0 + if leading_one[row_num - 1] > leading_one_pos: + # The previous first one was further to the right + return 0 + + # Count the number of non-zero values in the column that is the + # leading one. It should be one (this row) + if sum(1 for row in matrix if row[leading_one_pos] != 0) != 1: + return 0 + + return 1 + + +def main(): + # Convert input into a list of list of integers + matrix = json.loads(sys.argv[1]) + result = echelon(matrix) + print(result) + + +if __name__ == '__main__': + main() diff --git a/challenge-257/sgreen/python/test.py b/challenge-257/sgreen/python/test.py new file mode 100755 index 0000000000..2e50bc4231 --- /dev/null +++ b/challenge-257/sgreen/python/test.py @@ -0,0 +1,26 @@ +#!/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([5, 2, 1, 6]), [2, 1, 0, 3]) + self.assertEqual(ch_1.smaller_than_current([1, 2, 0, 3]), [1, 2, 0, 3]) + self.assertEqual(ch_1.smaller_than_current([0, 1]), [0, 1]) + self.assertEqual(ch_1.smaller_than_current([9, 4, 9, 2]), [2, 1, 2, 0]) + + def test_ch_2(self): + self.assertEqual(ch_2.echelon( + [[1, 0, 0, 1], [0, 1, 0, 2], [0, 0, 1, 3]]), 1) + self.assertEqual(ch_2.echelon([[1, 1, 0], [0, 1, 0], [0, 0, 0]]), 0) + self.assertEqual(ch_2.echelon( + [[0, 1, -2, 0, 1], [0, 0, 0, 1, 3], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]), 1) + self.assertEqual(ch_2.echelon( + [[1, 0, 0, 4], [0, 1, 0, 7], [0, 0, 1, -1]]), 1) + + +if __name__ == '__main__': + unittest.main() |
