aboutsummaryrefslogtreecommitdiff
path: root/challenge-211/spadacciniweb
diff options
context:
space:
mode:
author冯昶 <fengchang@novel-supertv.com>2023-04-10 18:31:25 +0800
committer冯昶 <fengchang@novel-supertv.com>2023-04-10 18:31:25 +0800
commit6b7fd386eb2e4b8b700fb12f6a7ca043baa6164c (patch)
tree5a04aa69760f2e25d68ed407c5c7fd9867e7ebcc /challenge-211/spadacciniweb
parent1b193b53c1db2f37be5ccca5ce8ab4545df4f607 (diff)
parent4eb9782a746173721822a9ffa29d6f14297a6dca (diff)
downloadperlweeklychallenge-club-6b7fd386eb2e4b8b700fb12f6a7ca043baa6164c.tar.gz
perlweeklychallenge-club-6b7fd386eb2e4b8b700fb12f6a7ca043baa6164c.tar.bz2
perlweeklychallenge-club-6b7fd386eb2e4b8b700fb12f6a7ca043baa6164c.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-211/spadacciniweb')
-rw-r--r--challenge-211/spadacciniweb/go/ch-1.go59
-rw-r--r--challenge-211/spadacciniweb/perl/ch-1.pl53
-rw-r--r--challenge-211/spadacciniweb/python/ch-1.py43
-rw-r--r--challenge-211/spadacciniweb/ruby/ch-1.rb40
4 files changed, 195 insertions, 0 deletions
diff --git a/challenge-211/spadacciniweb/go/ch-1.go b/challenge-211/spadacciniweb/go/ch-1.go
new file mode 100644
index 0000000000..97d6d47354
--- /dev/null
+++ b/challenge-211/spadacciniweb/go/ch-1.go
@@ -0,0 +1,59 @@
+/*
+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
+*/
+
+package main
+
+import (
+ "fmt"
+)
+
+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
+ }
+ }
+ return true
+}
+
+func main() {
+ matrix1 := [][]int{
+ {4, 3, 2, 1},
+ {5, 4, 3, 2},
+ {6, 5, 4, 3},
+ }
+ fmt.Println(check_toeplitz(matrix1))
+
+ matrix2 := [][]int{
+ {1, 2, 3},
+ {3, 2, 1},
+ }
+ fmt.Println(check_toeplitz(matrix2))
+}
diff --git a/challenge-211/spadacciniweb/perl/ch-1.pl b/challenge-211/spadacciniweb/perl/ch-1.pl
new file mode 100644
index 0000000000..78e264778b
--- /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);
+
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))
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