-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pramp-interview1: toeplitz matrix (#10)
* extract print and assertion functions into common * pramp: toeplitz-matrix => initial setup with unit tests and logging * add print matrix utils function * toeplitz matrix => add additional test case for non square matrix * toeplitz matrix => solve with O(nxm) runtime complexity and O(1) space complexity * toepliz matrix => remove console log * toeplitz-matrix => add additional test cases
- Loading branch information
Showing
3 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const testMatrixes = [ | ||
{ | ||
params: [], | ||
expectedResult: false, | ||
label: 'empty-array' | ||
}, | ||
{ | ||
params: [[1, 2, 3, 4], | ||
[5, 1, 2, 3], | ||
[6, 5, 1, 2]], | ||
expectedResult: true, | ||
label: 'toeplitz-matrix' | ||
}, | ||
{ | ||
params: [[1, 2, 3, 4], | ||
[5, 1, 9, 3], | ||
[6, 5, 1, 2]], | ||
expectedResult: false, | ||
label: 'non-toeplitz-matrix1' | ||
}, | ||
{ | ||
params: [[1, 2, 3, 4], | ||
[5, 1, 2, 3], | ||
[6, 4, 1, 2]], | ||
expectedResult: false, | ||
label: 'non-toeplitz-matrix2' | ||
}, | ||
{ | ||
params: [[3, 7, 0, 9, 8], | ||
[5, 3, 7, 0, 9], | ||
[6, 5, 3, 7, 0], | ||
[4, 6, 5, 3, 7]], | ||
expectedResult: true, | ||
label: 'toeplitz-matrix2' | ||
}, { | ||
params: [[4, 0], [9, 4]], | ||
expectedResult: true, | ||
label: 'toeplitz-matrix3' | ||
}, | ||
]; | ||
|
||
module.exports = { | ||
testMatrixes, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const common = require('../../../common/utils.js'); | ||
const testCases = require('./testData/index.js'); | ||
|
||
/* | ||
* Arguments: | ||
* matrix: Array[i][j] | ||
* | ||
* 0 ≤ matrix.length ≤ 20 | ||
* 0 ≤ matrix[i].length ≤ 20 | ||
* 0 ≤ matrix[i][j] ≤ 20 | ||
* NB: matrix is not necessarily square, i.e. with the same number of columns and rows | ||
* | ||
* returns boolean if matrix is of type Toeplitz, i.e. has the same number on its descending diagonal from left to rigth | ||
* | ||
* eg. | ||
* const matrix = [[1,2,3,4], | ||
* [5,1,2,3], | ||
* [6,5,1,2]]; | ||
* isToeplitzMatrix(matrix) | ||
* returns true | ||
* | ||
* const matrix = [[1,2,3,4], | ||
* [5,1,9,3], | ||
* [6,5,1,2]]; | ||
* | ||
* isToeplitzMatrix(matrix) | ||
* returns false | ||
*/ | ||
|
||
function isToeplitzMatrix(matrix = []) { | ||
const diagonalStart = matrix[0] && matrix[0][0] || ''; | ||
|
||
if (!diagonalStart) return false; | ||
|
||
for (let i = 0; i < matrix.length - 1; i++) { // iterate over rows | ||
for (let j = 0; j < matrix[0].length - 1; j++) { // iterate over columns | ||
if (matrix[i][j] !== matrix[i + 1][j + 1]) { | ||
return false; | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
|
||
common.printTestResult(isToeplitzMatrix, testCases.testMatrixes, 'result to the question is it a toeplitz matrix ?'); |