aboutsummaryrefslogtreecommitdiff
path: root/challenge-266/sgreen/python/ch-2.py
blob: d0df7257ee9a1171a176de72335e674f7f7462dc (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
#!/usr/bin/env python3

import json
import sys


def x_matrix(matrix: list) -> bool:
    """Determine if the matrix is a X Matrix

    Args:
        matrix (list): The supplied matrix

    Returns:
        bool: Wether the matrix as a X Matrix or not
    """
    rows = len(matrix)

    # Check we have a square
    for row in range(rows):
        if len(matrix[row]) != rows:
            raise ValueError("Please specify a square matrix")

    # Check that all the values are correct
    for row in range(rows):
        for col in range(rows):
            if col == row or col == rows - 1 - row:
                # We are expecting a non-zero value
                if matrix[row][col] == 0:
                    return False
            elif matrix[row][col] != 0:
                # We are expecting a zero value
                return False

    return True


def main():
    # Parse the matrix from the input
    matrix = json.loads(sys.argv[1])
    result = x_matrix(matrix)
    print('true' if result else 'false')


if __name__ == '__main__':
    main()