blob: 3cfe7dbe3e92234095428f56e2ac86c159728369 (
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
|
#!/usr/bin/env python3
import json
import sys
def special_positions(matrix: list) -> int:
rows = len(matrix)
cols = len(matrix[0])
special_position = 0
# Count the number of ones in each row and column
row_count = [0] * rows
col_count = [0] * cols
for row in range(rows):
# Check this has the same number of columns as the first row
if len(matrix[row]) != cols:
raise ValueError("Row %s has the wrong number of columns", row)
for col in range(cols):
if matrix[row][col]:
row_count[row] += 1
col_count[col] += 1
# Find the number of special positions. This is true if the value is one
# and the row_count and col_count is one
for row in range(rows):
for col in range(cols):
if matrix[row][col] and row_count[row] == 1 and col_count[col] == 1:
special_position += 1
return special_position
def main():
matrix = json.loads(sys.argv[1])
result = special_positions(matrix)
print(result)
if __name__ == '__main__':
main()
|