aboutsummaryrefslogtreecommitdiff
path: root/challenge-257/sgreen/python/ch-2.py
blob: 04eccc0243b1c5d23f291b79f83d3a7af043c724 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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()