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

import json
import sys


def main(matrix):
    # Calculate the size of the matrix
    rows = len(matrix)
    cols = len(matrix[0])

    # Check that all columns are of equal length
    for r in range(1, rows):
        if len(matrix[r]) != cols:
            raise ValueError(f'Row {r} has different number of columns')

    print('[')
    for r in range(rows-1):
        # Compute each row
        row = []
        for c in range(cols-1):
            row.append(matrix[r][c] + matrix[r][c+1] +
                       matrix[r+1][c] + matrix[r+1][c+1])
        print('  ' + str(row) + ',')

    print(']')


if __name__ == '__main__':
    # Read the input as JSON
    main(json.loads(sys.argv[1]))