diff options
| author | Steven <steven1170@zoho.eu> | 2024-05-20 16:31:34 +0100 |
|---|---|---|
| committer | Steven <steven1170@zoho.eu> | 2024-05-20 16:31:34 +0100 |
| commit | 2bfcb5f9fd5227bbcb92f1520733bc7fc027e670 (patch) | |
| tree | 2806e8104750ce16c38ce09fca70710d66f84648 | |
| parent | ed462bf99ed6fda013ab14d58855951ef13b05fa (diff) | |
| download | perlweeklychallenge-club-2bfcb5f9fd5227bbcb92f1520733bc7fc027e670.tar.gz perlweeklychallenge-club-2bfcb5f9fd5227bbcb92f1520733bc7fc027e670.tar.bz2 perlweeklychallenge-club-2bfcb5f9fd5227bbcb92f1520733bc7fc027e670.zip | |
add solution week 270 tast 1 in python
| -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) |
