code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

048 Extract Matrix Column

/**
 * 
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

/* 
Given a rectangular matrix and an integer column, return an array containing the elements of the columnth column of the given matrix (the leftmost column is the 0th one).

Example

For matrix = [[1, 1, 1, 2], [0, 5, 0, 4], [2, 1, 3, 6]]

      and column = 2, the output should be
      extractMatrixColumn(matrix, column) = [1, 0, 3].
*/

function extractMatrixColumn(matrix, k) {
  let column = []
  for (let index = 0; index < matrix.length; index++) {
    column.push(matrix[index][k])    
  }
  return column
}

test('extract Matrix Column', () => {
  expect(extractMatrixColumn([[1, 1, 1, 2], [0, 5, 0, 4], [2, 1, 3, 6]], 2)).toEqual([1,0,3])
});

Created 2019-12-14T23:23:16+05:30 · Edit