How?
Given a matrix, a, for
each (v
) of the transpose, zip(*a)
, and a
itself we find the height required (given the transpose this is the width).
Mapping max
across v
yields a list of zeros and ones, representing if each row of v
contains any ones. The string representation of this list is found using backticks (`...`
), this gives a string with a leading [
, then the zeros and ones delimited by ,
. We slice this string starting a index one in steps of three using [1::3]
getting us a string of only the zeros and ones, which allows us to use the string function strip
to remove the outer zeros (strip('0')
).
For example:
a = [[0,0,0,0,0] map(max,a) = [0,1,1]
,[0,1,0,0,0] `map(max,a)`[1::3] = '011'
,[0,0,0,1,0]] `map(max,a)`[1::3].strip('0') = '11'
len(`map(max,a)`[1::3].strip('0')) = 2
zip(*a) = [(0,0,0) map(max,zip(*a)) = [0,1,0,1,0]
,(0,1,0) `map(max,zip(*a))`[1::3] = '01010'
,(0,0,0) `map(max,zip(*a))`[1::3].strip('0') = '101'
,(0,0,1) len(`map(max,zip(*a))`[1::3].strip('0')) = 3
,(0,0,0)]
--> [len(`map(max,v)`[1::3].strip('0'))for v in zip(*a),a] = [3,2]