Skip to content

Commit

Permalink
pramp-interview1: toeplitz matrix (#10)
Browse files Browse the repository at this point in the history
* 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
VeronicaM authored May 9, 2020
1 parent 5feb79b commit 9e19eec
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 1 deletion.
63 changes: 62 additions & 1 deletion common/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const colors = require('colors');

const isArrayEqual = (arr1, arr2) => {
if (arr1.length !== arr2.length) return false;

Expand All @@ -22,7 +24,66 @@ const isArrayOfObjectsEqual = (arr1, arr2) => {
return true;
};


function assert(params, result, expected, expectedResultLabel, checkType) {
const resultMsg = `Passed in params were ${JSON.stringify(params)} and the expected ${expectedResultLabel} is ${expected}`;

let checkCondition;

switch (checkType) {
case 'array':
checkCondition = isArrayEqual(result, expected);
break;
case 'array-of-objects':
checkCondition = isArrayOfObjectsEqual(result, expected);
break;
default:
checkCondition = result === expected;

}

if (checkCondition) {
console.log(resultMsg.green)
return true;
} else {
console.error(`${resultMsg}, found ${result}`.brightRed);
return false;
}
}

function printTestResult(solutionFunc, testData, expectedResultLabel, checkType = '') {
let count = 0;

testData.forEach(function loopUnitTests({ params, expectedResult, label }, index) {
console.log(`${index + 1}. ${label}`.yellow);

const isCorrect = assert(params, solutionFunc(params), expectedResult, expectedResultLabel, checkType);

if (!isCorrect) {
count += 1;
}
});

console.error(`You have ${count}/${testData.length} failling tests`.brightRed);
console.error(`You have ${testData.length - count}/${testData.length} passing tests. Congrats!!!`.green);
}

function printMatrix(matrix) {
for (let colIndex = 0; colIndex < matrix.length; colIndex++) { // iterate over columns
let rowString = '';

for (let rowIndex = 0; rowIndex < matrix[0].length; rowIndex++) { // iterate over rows
rowString += `${matrix[colIndex][rowIndex]}`;
}

console.log(`${rowString}`);
}
console.log('\n');
}

module.exports = {
isArrayEqual,
isArrayOfObjectsEqual
isArrayOfObjectsEqual,
printTestResult,
printMatrix
}
44 changes: 44 additions & 0 deletions interviews/pramp/toeplitz-matrix/testData/index.js
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,
};
47 changes: 47 additions & 0 deletions interviews/pramp/toeplitz-matrix/toeplitz-matrix.js
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 ?');

0 comments on commit 9e19eec

Please sign in to comment.