diff options
| -rw-r--r-- | challenge-270/steven-wilson/python/ch-1.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/challenge-270/steven-wilson/python/ch-1.py b/challenge-270/steven-wilson/python/ch-1.py new file mode 100644 index 0000000000..c258062bfa --- /dev/null +++ b/challenge-270/steven-wilson/python/ch-1.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +from collections import Counter + + +def special_position(matrix): + ''' Given a m x n binary matrix, return the number of special positions in + the given binary matrix. + + A position (i, j) is called special if $matrix[i][j] == 1 and all other + elements in the row i and column j are 0. + + >>> special_position([[1, 0, 0], [0, 0, 1], [1, 0, 0],]) + 1 + >>> special_position([ [1, 0, 0], [0, 1, 0], [0, 0, 1], ]) + 3 + ''' + possible = [row.index(1) for row in matrix if sum(row) == 1] + counter = Counter(possible) + return sum(1 for value in counter.values() if value == 1) + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) |
