aboutsummaryrefslogtreecommitdiff
path: root/challenge-211
diff options
context:
space:
mode:
authorMariano Spadaccini <spadacciniweb@gmail.com>2023-04-11 16:08:23 +0200
committerMariano Spadaccini <spadacciniweb@gmail.com>2023-04-11 16:08:23 +0200
commitbb2cf0104d8ae09e93b8ab7621b8c400640311e3 (patch)
tree954cca1c21fdc07f58af2afa2ae70d224f1b360c /challenge-211
parente5e6913d2faf5607a52ceb96efaa35c7b6ce57d3 (diff)
downloadperlweeklychallenge-club-bb2cf0104d8ae09e93b8ab7621b8c400640311e3.tar.gz
perlweeklychallenge-club-bb2cf0104d8ae09e93b8ab7621b8c400640311e3.tar.bz2
perlweeklychallenge-club-bb2cf0104d8ae09e93b8ab7621b8c400640311e3.zip
PWC-212 (and PWC-211 fix)
Diffstat (limited to 'challenge-211')
-rw-r--r--challenge-211/spadacciniweb/go/ch-1.go13
-rw-r--r--challenge-211/spadacciniweb/perl/ch-1.pl30
-rw-r--r--challenge-211/spadacciniweb/python/ch-1.py10
-rw-r--r--challenge-211/spadacciniweb/ruby/ch-1.rb9
4 files changed, 29 insertions, 33 deletions
diff --git a/challenge-211/spadacciniweb/go/ch-1.go b/challenge-211/spadacciniweb/go/ch-1.go
index 97d6d47354..b19067b0e7 100644
--- a/challenge-211/spadacciniweb/go/ch-1.go
+++ b/challenge-211/spadacciniweb/go/ch-1.go
@@ -30,14 +30,11 @@ import (
func check_toeplitz(m [][]int) bool {
rows := len(m)
cols := len(m[0])
- var dimens int = rows
- if (cols < rows) {
- dimens = cols
- }
- val := m[0][0]
- for i := 0; i < dimens; i++ {
- if m[i][i] != val {
- return false
+ for i := 1; i < rows; i++ {
+ for j := 1; j < cols; j++ {
+ if m[i][j] != m[i-1][j-1] {
+ return false
+ }
}
}
return true
diff --git a/challenge-211/spadacciniweb/perl/ch-1.pl b/challenge-211/spadacciniweb/perl/ch-1.pl
index 78e264778b..23550611f8 100644
--- a/challenge-211/spadacciniweb/perl/ch-1.pl
+++ b/challenge-211/spadacciniweb/perl/ch-1.pl
@@ -26,28 +26,26 @@ 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;
+ my @matrix = @_;
+ my $rows = scalar @matrix;
+ my $cols = scalar @{$matrix[0]};
+ foreach my $i (1..($rows-1)) {
+ foreach my $j (1..($cols-1)) {
+ return 'false'
+ unless $matrix[$i][$j] == $matrix[$i-1][$j-1];
+ }
}
return 'true';
}
-my $matrix = [ [4, 3, 2, 1],
+my @matrix = ( [4, 3, 2, 1],
[5, 4, 3, 2],
[6, 5, 4, 3],
- ];
-say check_toeplitz($matrix);
+ );
+say check_toeplitz(@matrix);
-$matrix = [ [1, 2, 3],
+@matrix = ( [1, 2, 3],
[3, 2, 1],
- ];
-say check_toeplitz($matrix);
+ );
+say check_toeplitz(@matrix);
diff --git a/challenge-211/spadacciniweb/python/ch-1.py b/challenge-211/spadacciniweb/python/ch-1.py
index 00f663b9f1..985e105c76 100644
--- a/challenge-211/spadacciniweb/python/ch-1.py
+++ b/challenge-211/spadacciniweb/python/ch-1.py
@@ -23,11 +23,11 @@ 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'
+ rows, cols = m.shape
+ for i in range(1,rows):
+ for j in range(1,cols):
+ if m[i][j] != m[i-1][j-1]:
+ return 'false'
return 'true'
if __name__ == "__main__":
diff --git a/challenge-211/spadacciniweb/ruby/ch-1.rb b/challenge-211/spadacciniweb/ruby/ch-1.rb
index 03b84d9ff3..c24d762b7a 100644
--- a/challenge-211/spadacciniweb/ruby/ch-1.rb
+++ b/challenge-211/spadacciniweb/ruby/ch-1.rb
@@ -20,10 +20,11 @@
# 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
+ rows, cols = m.length, m[0].length
+ (1...rows).each { |i|
+ (1...cols).each { |j|
+ return 'false' if m[i][j] != m[i-1][j-1]
+ }
}
return 'true'
end