aboutsummaryrefslogtreecommitdiff
path: root/challenge-011/athanasius/perl5/ch-2.pl
diff options
context:
space:
mode:
authordrclaw1394 <drclaw@mac.com>2019-06-10 20:56:34 +1000
committerGitHub <noreply@github.com>2019-06-10 20:56:34 +1000
commit5c1663e3f7a4bf8767cd62c4e7db03186561edb2 (patch)
tree2a0fa3af0f3b0fd5a71a9ae67984f4fa2adb8aa1 /challenge-011/athanasius/perl5/ch-2.pl
parentb577c5e4a0a63be26d335cb74580c1ae721105c0 (diff)
parente874646e75a09fbe9c9f248e2071bc4182973b7f (diff)
downloadperlweeklychallenge-club-5c1663e3f7a4bf8767cd62c4e7db03186561edb2.tar.gz
perlweeklychallenge-club-5c1663e3f7a4bf8767cd62c4e7db03186561edb2.tar.bz2
perlweeklychallenge-club-5c1663e3f7a4bf8767cd62c4e7db03186561edb2.zip
Merge pull request #11 from manwar/master
update to week 12
Diffstat (limited to 'challenge-011/athanasius/perl5/ch-2.pl')
-rw-r--r--challenge-011/athanasius/perl5/ch-2.pl78
1 files changed, 78 insertions, 0 deletions
diff --git a/challenge-011/athanasius/perl5/ch-2.pl b/challenge-011/athanasius/perl5/ch-2.pl
new file mode 100644
index 0000000000..7f6a6c727e
--- /dev/null
+++ b/challenge-011/athanasius/perl5/ch-2.pl
@@ -0,0 +1,78 @@
+#!perl
+
+################################################################################
+=comment
+
+Perl Weekly Challenge 011
+=========================
+
+Challenge #2
+------------
+
+Write a script to create an Indentity Matrix for the given size. For example, if
+the size is 4, then create Identity Matrix 4x4. For more information about
+*Indentity Matrix*, please read the
+[ https://en.wikipedia.org/wiki/Identity_matrix |wiki] page.
+
+=cut
+################################################################################
+
+#--------------------------------------#
+# Copyright © 2019 Perlmonk Athanasius #
+#--------------------------------------#
+
+use strict;
+use warnings;
+use Const::Fast;
+use Scalar::Util::Numeric qw( isint );
+
+const my $DEFAULT_SIZE => 4;
+const my $MAX_SIZE => 39; # Adjust for the target console
+
+$| = 1;
+
+MAIN:
+{
+ my $size = $ARGV[0] // $DEFAULT_SIZE;
+ my $matrix = identity_matrix($size);
+
+ print "\nIdentity Matrix of size $size:\n";
+ print_matrix($matrix);
+}
+
+sub identity_matrix
+{
+ my ($size) = @_;
+
+ die "Invalid matrix size $size, stopped"
+ unless isint($size) && $size > 0;
+
+ die "Matrix size $size is too wide to be properly displayed, stopped"
+ if $size > $MAX_SIZE;
+
+ my $matrix;
+
+ for my $i (0 .. $size - 1)
+ {
+ $matrix->[$i][$_] = ($i == $_) ? 1 : 0 for 0 .. $size - 1;
+ }
+
+ return $matrix;
+}
+
+sub print_matrix
+{
+ my ($matrix) = @_;
+ my $size = scalar @$matrix;
+
+ if ($size == 1)
+ {
+ print '[', $matrix->[0][0], "]\n";
+ }
+ else
+ {
+ print '|', join( ' ', $matrix->[$_]->@* ), "|\n" for 0 .. $size - 1;
+ }
+}
+
+################################################################################