Created
January 2, 2018 01:28
-
-
Save earlonrails/ef049c77027a0ec6d0d9fe266254919f to your computer and use it in GitHub Desktop.
Check to see if a matrix is a Toeplitz matrix https://www.geeksforgeeks.org/find-if-given-matrix-is-toeplitz-or-not ES6
This file contains hidden or 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
#!/usr/bin/env node | |
// https://www.geeksforgeeks.org/find-if-given-matrix-is-toeplitz-or-not/ | |
class Toeplitz { | |
static check(matrix) { | |
for (var i = 1; i < matrix.length - 1; i++) { | |
let row = matrix[i] | |
let prevRow = matrix[i - 1] | |
for (var j = 1; j < row.length - 1; j++) { | |
let diagonalParent = prevRow[j - 1] | |
if (diagonalParent != row[j]) { | |
return false | |
} | |
} | |
} | |
return true | |
} | |
} | |
var isToe = [ | |
[6, 7, 8, 9], | |
[4, 6, 7, 8], | |
[1, 4, 6, 7], | |
[0, 1, 4, 6], | |
[2, 0, 1, 4] | |
] | |
console.log("Should be a Toeplitz:", Toeplitz.check(isToe)) | |
var isNotToe = [ | |
[6, 7, 8, 9], | |
[4, 6, 6, 8], | |
[1, 2, 6, 7], | |
[0, 1, 4, 6], | |
[2, 8, 1, 4] | |
] | |
console.log("Should be a Toeplitz:", Toeplitz.check(isNotToe)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment