Skip to content

Instantly share code, notes, and snippets.

@earlonrails
Created January 2, 2018 01:28
Show Gist options
  • Save earlonrails/ef049c77027a0ec6d0d9fe266254919f to your computer and use it in GitHub Desktop.
Save earlonrails/ef049c77027a0ec6d0d9fe266254919f to your computer and use it in GitHub Desktop.

Revisions

  1. earlonrails created this gist Jan 2, 2018.
    39 changes: 39 additions & 0 deletions Toeplitz.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    #!/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))