diff options
| author | Mariano Spadaccini <spadacciniweb@gmail.com> | 2023-04-06 11:18:19 +0200 |
|---|---|---|
| committer | Mariano Spadaccini <spadacciniweb@gmail.com> | 2023-04-06 11:18:19 +0200 |
| commit | b757f502a9d57f5ddced1f5c1a9a93fa1ece27b1 (patch) | |
| tree | 3202d3d1e4534c82e3227196667795581f62553b /challenge-211 | |
| parent | ed17a0bef83e3276a3949814dab37a8a51871041 (diff) | |
| download | perlweeklychallenge-club-b757f502a9d57f5ddced1f5c1a9a93fa1ece27b1.tar.gz perlweeklychallenge-club-b757f502a9d57f5ddced1f5c1a9a93fa1ece27b1.tar.bz2 perlweeklychallenge-club-b757f502a9d57f5ddced1f5c1a9a93fa1ece27b1.zip | |
Add ch-1.pl
Diffstat (limited to 'challenge-211')
| -rw-r--r-- | challenge-211/spadacciniweb/perl/ch-1.pl | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/challenge-211/spadacciniweb/perl/ch-1.pl b/challenge-211/spadacciniweb/perl/ch-1.pl new file mode 100644 index 0000000000..4d69deaa17 --- /dev/null +++ b/challenge-211/spadacciniweb/perl/ch-1.pl @@ -0,0 +1,53 @@ +#!/usr/bin/env perl + +# 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 + +use strict; +use warnings; +use feature qw/say/; + +sub check_toeplitz {; + my $matrix = shift; + my $rows = @$matrix; + my $cols = scalar @{$matrix->[0]}; + my $min = ($rows < $cols) + ? $rows + : $cols; + my $value = $matrix->[0]->[0]; + foreach my $i (0..$min-1) { + return 'false' + unless $matrix->[$i]->[$i] == $value; + } + return 'true'; +} + +my $matrix = [ [4, 3, 2, 1], + [5, 4, 3, 2], + [6, 5, 4, 3], + ]; +say check_toeplitz($matrix); + +$matrix = [ [1, 2, 3], + [3, 2, 1], + ]; +say check_toeplitz($matrix); + |
