diff options
| author | Mariano Spadaccini <spadacciniweb@gmail.com> | 2023-04-06 11:33:16 +0200 |
|---|---|---|
| committer | Mariano Spadaccini <spadacciniweb@gmail.com> | 2023-04-06 11:33:16 +0200 |
| commit | 7e35f81f3255f235fc751cfaf30074b83a8b39a0 (patch) | |
| tree | c24e19e74674344452e26e299b7e162c738225f4 /challenge-211 | |
| parent | b757f502a9d57f5ddced1f5c1a9a93fa1ece27b1 (diff) | |
| download | perlweeklychallenge-club-7e35f81f3255f235fc751cfaf30074b83a8b39a0.tar.gz perlweeklychallenge-club-7e35f81f3255f235fc751cfaf30074b83a8b39a0.tar.bz2 perlweeklychallenge-club-7e35f81f3255f235fc751cfaf30074b83a8b39a0.zip | |
Add ch-1.py
Diffstat (limited to 'challenge-211')
| -rw-r--r-- | challenge-211/spadacciniweb/python/ch-1.py | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/challenge-211/spadacciniweb/python/ch-1.py b/challenge-211/spadacciniweb/python/ch-1.py new file mode 100644 index 0000000000..00f663b9f1 --- /dev/null +++ b/challenge-211/spadacciniweb/python/ch-1.py @@ -0,0 +1,43 @@ +# Task 1: Toeplitz Matrix +# Submitted by: Mohammad S Anwar +# +# You are given a matrix m x n. +# Write a script to find out if the given matrix is Toeplitz Matrix. +# A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements. +# +# +# Example 1 +# Input: @matrix = [ [4, 3, 2, 1], +# [5, 4, 3, 2], +# [6, 5, 4, 3], +# ] +# Output: true +# +# Example 2 +# Input: @matrix = [ [1, 2, 3], +# [3, 2, 1], +# ] +# Output: false + +import numpy + +def check_toeplitz(matrix): + m = numpy.array(matrix) + dim = min(m.shape) + value = m[0][0] + for i in range(dim): + if m[i][i] != value: + return 'false' + return 'true' + +if __name__ == "__main__": + matrix = [ [4, 3, 2, 1], + [5, 4, 3, 2], + [6, 5, 4, 3], + ] + print(check_toeplitz(matrix)) + + matrix = [ [1, 2, 3], + [3, 2, 1], + ] + print(check_toeplitz(matrix)) |
