Column with a few zeros X85208


Statement
 

pdf   zip

thehtml

Write a function zcol(mat) that receives an n× m matrix of integers mat and finds the first column with at least a 10% of zeros with respect to the column size, returning the value of that column at the first row. If no such column exists, it must return the sum of all the elements of the matrix. You can count on both the number of rows and the number of columns being strictly positive.

Observation

You can safely assume only that the rows of the matrix are either tuples or lists, and that the matrix itself is either a tuple of rows or a list of rows.

Sample session
>>> zcol([(2, 3, 4), (0, 1, 5), (-1, 0, 6)])
2
>>> zcol(([1, 2, 0], [7, 10, -7], [102, 101, 100]))
0
>>> zcol([(10, 20), (11, 21), (12, 22), (13, 23), (0, 24), (15, 25), (16, 0), (17, 27), (18, 28), (19, 29), (20, 30)])
400
>>> zcol(([10, 20], [11, 21], [12, 22], [13, 23], [0, 0], [15, 25], [16, 24], [17, 27], [18, 28], [19, 29], [20, 30]))
400
Information
Author
José Luis Balcázar
Language
English
Official solutions
Python
User solutions
Python