Skip to content

Instantly share code, notes, and snippets.

@earlonrails
Created January 2, 2018 01:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save earlonrails/ef049c77027a0ec6d0d9fe266254919f to your computer and use it in GitHub Desktop.
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
#!/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