From 2bfcb5f9fd5227bbcb92f1520733bc7fc027e670 Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 20 May 2024 16:31:34 +0100 Subject: add solution week 270 tast 1 in python --- challenge-270/steven-wilson/python/ch-1.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 challenge-270/steven-wilson/python/ch-1.py 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) -- cgit