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.