aboutsummaryrefslogtreecommitdiff
path: root/challenge-011/athanasius/perl5/ch-2.pl
diff options
context:
space:
mode:
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;
+ }
+}
+
+################################################################################