diff options
| author | Mariano Spadaccini <spadacciniweb@gmail.com> | 2023-04-06 11:57:00 +0200 |
|---|---|---|
| committer | Mariano Spadaccini <spadacciniweb@gmail.com> | 2023-04-06 11:57:00 +0200 |
| commit | 3457c7a15b48e32fa9eb55630657287942ca0d98 (patch) | |
| tree | f75af2c5a0f19670b74d7e293ec7f73e214085f2 /challenge-211 | |
| parent | 7e35f81f3255f235fc751cfaf30074b83a8b39a0 (diff) | |
| download | perlweeklychallenge-club-3457c7a15b48e32fa9eb55630657287942ca0d98.tar.gz perlweeklychallenge-club-3457c7a15b48e32fa9eb55630657287942ca0d98.tar.bz2 perlweeklychallenge-club-3457c7a15b48e32fa9eb55630657287942ca0d98.zip | |
Add ch-1.rb
Diffstat (limited to 'challenge-211')
| -rw-r--r-- | challenge-211/spadacciniweb/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-211/spadacciniweb/ruby/ch-1.rb | 40 |
2 files changed, 41 insertions, 1 deletions
diff --git a/challenge-211/spadacciniweb/perl/ch-1.pl b/challenge-211/spadacciniweb/perl/ch-1.pl index 4d69deaa17..78e264778b 100644 --- a/challenge-211/spadacciniweb/perl/ch-1.pl +++ b/challenge-211/spadacciniweb/perl/ch-1.pl @@ -25,7 +25,7 @@ use strict; use warnings; use feature qw/say/; -sub check_toeplitz {; +sub check_toeplitz { my $matrix = shift; my $rows = @$matrix; my $cols = scalar @{$matrix->[0]}; diff --git a/challenge-211/spadacciniweb/ruby/ch-1.rb b/challenge-211/spadacciniweb/ruby/ch-1.rb new file mode 100644 index 0000000000..03b84d9ff3 --- /dev/null +++ b/challenge-211/spadacciniweb/ruby/ch-1.rb @@ -0,0 +1,40 @@ +# 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 + +def check_toeplitz(m) + rc = [m.length, m[0].length].min - 1 + val = m[0][0] + (0..rc).each {|e| + return 'false' if m[e][e] != val + } + return 'true' +end + +matrix = [ [4, 3, 2, 1], + [5, 4, 3, 2], + [6, 5, 4, 3], + ] +puts check_toeplitz matrix + +matrix = [ [1, 2, 3], + [3, 2, 1], + ] +puts check_toeplitz matrix |
