aboutsummaryrefslogtreecommitdiff
path: root/challenge-211/spadacciniweb/ruby
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2023-04-09 19:01:48 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2023-04-09 19:01:48 +0100
commitc5c9938bcabccd143a967d74a9fc135beeee8002 (patch)
treeeb3fd8e67019cea8e1a1b30c8147478292025796 /challenge-211/spadacciniweb/ruby
parentf03ec2c10499edf8b77ea0c3831728853c20e983 (diff)
parent4890cd1addbde634e231ba6eb4656f7eb59085e9 (diff)
downloadperlweeklychallenge-club-c5c9938bcabccd143a967d74a9fc135beeee8002.tar.gz
perlweeklychallenge-club-c5c9938bcabccd143a967d74a9fc135beeee8002.tar.bz2
perlweeklychallenge-club-c5c9938bcabccd143a967d74a9fc135beeee8002.zip
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-211/spadacciniweb/ruby')
-rw-r--r--challenge-211/spadacciniweb/ruby/ch-1.rb40
1 files changed, 40 insertions, 0 deletions
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