1

I am trying to convert(as close to original(Python in this case)) as possible the following function from Python to JS.

def getCol(mat, col):
    return [mat[i][col] for i in range(3)]

def isSubMatrixFull(mat):
    n = len(mat[0])
    ans = [False]*(n-2)
    kernel = getCol(mat, 0) + getCol(mat, 1) + getCol(mat, 2) # O(1)
    for i in range(n - 2): # O(n)
        if len(set(kernel)) == 9: # O(1)
            ans[i] = True # O(1)
        if i < n - 3: # O(1)
            kernel = kernel[3:] + getCol(mat, i + 3) # O(1)
    return ans

nums = [[1, 2, 3, 2, 5, 7], [4, 5, 6, 1, 7, 6], [7, 8, 9, 4, 8, 3]]
print(isSubMatrixFull(nums)) 

So far, I have come to this:

function isSubMatrixFull(mat) {
  
  let test = new Array();
  function getCol(mat, col) {
    for (let i in mat) {
      test.push([mat[i][col]]);
     
       //  I have to find a way, to basically assign test values to [mat[i][col]]
       //   and then send it back to getCol(mat, i + 3)
    }
  }

  getCol(mat, 3);
  // The below would be unnecessary if I would have managed to assign test values(or 
  //  would have found other way) to [mat[i][col]] and send back to getCol(mat, i + 3) 
  test = test
    .map((v) => v)
    .join("")
    .split("")
    .map(Number);

  let n = mat[0].length;
  let res = new Array(n - 2).fill(false);
  let main = mat
    .map((rowValue, rowIndex) => mat.map((val) => val[rowIndex]))
    .join(",")
    .split(",");
  for (let i = 0; i < n - 2; i++) {
    if (new Set(main).size == 9) {
      res[i] = true;
    }
    if (i < n - 3) {
      main = main.slice(3) + getCol(mat, i + 3);
    }
  }
  // return res;
}

console.log(
  isSubMatrixFull([
    [1, 2, 3, 2, 5, 7],
    [4, 5, 6, 1, 7, 6],
    [7, 8, 9, 4, 8, 3],
  ])
);

The **output** of the above functions' input should be
isSubmatrixFull(numbers) = [true, false, true, false]

Please see here for more info about this question: https://leetcode.com/discuss/interview-question/860490/codesignal-intern-issubmatrixfull

Please read the comments(inside code) to see what I have done and trying to do.

E_net4
  • 27,810
  • 13
  • 101
  • 139
tseeker
  • 79
  • 9
  • I provided a preliminary answer. If you can add some examples of desired inputs and outputs to your question, then I can double check the answer and confirm that it's working. – nullromo Sep 14 '21 at 22:30
  • Hello, nullromo! Please see the question again, I have updated it with an answer to your question! – tseeker Sep 14 '21 at 22:56

1 Answers1

1

This code is a direct port from the python. It works for the example provided.

const getColumn = (matrix, column) => {
    return [...Array(3).keys()].map((i) => {
        return matrix[i][column];
    });
};

const computeKernel = (matrix) => {
    return [
        ...getColumn(matrix, 0),
        ...getColumn(matrix, 1),
        ...getColumn(matrix, 2),
    ];
};

const isSubMatrixFull = (matrix) => {
    const n = matrix[0].length;
    const answer = [...Array(n - 2)].map((_) => {
        return false;
    });
    let kernel = computeKernel(matrix);
    [...Array(n - 2).keys()].forEach((i) => {
        if (new Set(kernel).size === 9) {
            answer[i] = true;
        }
        if (i < n - 3) {
            kernel = [...kernel.slice(3), ...getColumn(matrix, i + 3)];
        }
    });
    return answer;
};

const numbers = [
    [1, 2, 3, 2, 5, 7],
    [4, 5, 6, 1, 7, 6],
    [7, 8, 9, 4, 8, 3],
];
console.log(isSubMatrixFull(numbers));
nullromo
  • 2,165
  • 2
  • 18
  • 39
  • Hello, nullromo! Please see the question again, I have updated it with an answer to your question! – tseeker Sep 14 '21 at 22:56
  • 1
    I tested it and it worked with your example case. (I had to change `length` to `size` though for the `Set`). – nullromo Sep 14 '21 at 22:59