Verify Valid n x n Matrix
Easy
2
62.5% Acceptance
In this lab, you will implement a function that verifies whether an n x n
matrix is valid or not. A matrix is valid if every row and every column contains all the integers from 1
to n
(inclusive).
Problem Statement
Given an n x n
integer matrix matrix
, return true
if the matrix is valid. Otherwise, return false
.
Example 1:
Input: matrix = [[1,2,3],[3,1,2],[2,3,1]] Output: true Explanation: In this case, n = 3, and every row and column contains the numbers 1, 2, and 3. Hence, we return true.
Example 2:
Input: matrix = [[1,1,1],[1,2,3],[1,2,3]] Output: false Explanation: In this case, n = 3, but the first row and the first column do not contain the numbers 2 or 3. Hence, we return false.
Constraints:
n == matrix.length == matrix[i].length
1 <= n <= 100
1 <= matrix[i][j] <= n
Challenges
- Export the
isValidMatrix
function fromindex.js
. - Implement the
isValidMatrix
function to check if the matrix is valid according to the above problem statement.
Evaluation script
1import fs from 'fs' 2 3const testlog = [] 4 5// Challenge 1 6try { 7 const isValidMatrix = await import('/home/damner/code/index.js').then(t => t.isValidMatrix) 8 9 if(isValidMatrix != undefined) { 10 testlog.push({ status: 'pass' }) 11 } else { 12 throw new Error('Function is not exported') 13 } 14} catch(error) { 15 testlog.push({ 16 status: 'error', 17 error: error.message || 'Challenge failed' 18 }) 19} 20 21// Challenge 2 22try { 23 const isValidMatrix = await import('/home/damner/code/index.js').then(t => t.isValidMatrix) 24 25 const matrix1 = [[1, 2, 3], [3, 1, 2], [2, 3, 1]] 26 const matrix2 = [[1, 1, 1], [1, 2, 3], [1, 2, 3]] 27 28 if(isValidMatrix(matrix1) === true && isValidMatrix(matrix2) === false) { 29 testlog.push({ status: 'pass' }) 30 } else { 31 throw new Error('Function implementation incorrect') 32 } 33} catch(error) { 34 testlog.push({ 35 status: 'error', 36 error: error.message || 'Challenge failed' 37 }) 38} 39 40fs.writeFileSync('/home/damner/code/.labtests/testlog.json', JSON.stringify(testlog)) 41fs.writeFileSync(process.env.UNIT_TEST_OUTPUT_FILE, JSON.stringify(testlog.map(result => result.status === 'pass')))
Setting up test environment script
1#!/bin/bash 2set -e 1 3 4mkdir -p /home/damner/code/.labtests 5 6cat > /home/damner/code/.labtests/package.json << EOF 7{ 8 "type": "module" 9} 10EOF 11 12cd /home/damner/code/.labtests 13mv $TEST_FILE_NAME ./nodecheck.test.js 14 15# import puppeteer doesn't work without it 16npm link puppeteer 17 18node nodecheck.test.js 2>&1 | tee evaluationscript.log
Initial file system for user
/** * @param {number[][]} matrix * @return {boolean} */ export function isValidMatrix(matrix) { };
{ "name": "codedamn-lab", "type": "module" }
tabs: ['index.js']
terminals: ['yarn install']