Skip to main content
added 638 characters in body
Source Link
Jonathan Allan
  • 110.1k
  • 7
  • 65
  • 282

Python 2, 63 63  55 bytes

-8 with help from Vincent (take the input matrix as a Numpy array)

lambda a:[len(`map(`a.max,v(x)`[1`[7::3].strip('0'))for vx in zip(*a)0,a]1]

An unnamed function accepting a list of lists2-d Numpy array of integers (in {0,1}) which returns a list of integers, [width,height].

Try it online!Try it online!


Non-Numpy version in 63 bytes (accepting a list of lists of integers in {0,1}):

lambda a:[len(`map(max,v)`[1::3].strip('0'))for v in zip(*a),a]

Try it online!

Python 2, 63 bytes

lambda a:[len(`map(max,v)`[1::3].strip('0'))for v in zip(*a),a]

An unnamed function accepting a list of lists of integers (in {0,1}) which returns a list of integers, [width,height].

Try it online!

Python 2,  63  55 bytes

-8 with help from Vincent (take the input matrix as a Numpy array)

lambda a:[len(`a.max(x)`[7::3].strip('0'))for x in 0,1]

An unnamed function accepting a 2-d Numpy array of integers (in {0,1}) which returns a list of integers, [width,height].

Try it online!


Non-Numpy version in 63 bytes (accepting a list of lists of integers in {0,1}):

lambda a:[len(`map(max,v)`[1::3].strip('0'))for v in zip(*a),a]

Try it online!

added 2 characters in body
Source Link
Jonathan Allan
  • 110.1k
  • 7
  • 65
  • 282

Given a matrix, aa, 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 , (comma+space). We slice this string starting aat 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')).

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')).

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 , (comma+space). We slice this string starting at 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')).

deleted 121 characters in body
Source Link
Jonathan Allan
  • 110.1k
  • 7
  • 65
  • 282

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]

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]
deleted 121 characters in body
Source Link
Jonathan Allan
  • 110.1k
  • 7
  • 65
  • 282
Loading
added 106 characters in body
Source Link
Jonathan Allan
  • 110.1k
  • 7
  • 65
  • 282
Loading
Post Undeleted by Jonathan Allan
added 27 characters in body
Source Link
Jonathan Allan
  • 110.1k
  • 7
  • 65
  • 282
Loading
Post Deleted by Jonathan Allan
Source Link
Jonathan Allan
  • 110.1k
  • 7
  • 65
  • 282
Loading