aboutsummaryrefslogtreecommitdiff
path: root/challenge-140
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2021-11-29 00:15:45 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2021-11-29 00:15:45 +0000
commitc40837e0976c9bce74758c620ebc12807f83f1b8 (patch)
treec421ad3101ec8f486a1abe24ba6275625e795a00 /challenge-140
parent27e9c02e67ab26014bb390e055d0fa5a9b954278 (diff)
downloadperlweeklychallenge-club-c40837e0976c9bce74758c620ebc12807f83f1b8.tar.gz
perlweeklychallenge-club-c40837e0976c9bce74758c620ebc12807f83f1b8.tar.bz2
perlweeklychallenge-club-c40837e0976c9bce74758c620ebc12807f83f1b8.zip
- Added Perl solution to "Multiplication Table" task of week 140.
Diffstat (limited to 'challenge-140')
-rw-r--r--challenge-140/mohammad-anwar/perl/ch-2.pl46
1 files changed, 46 insertions, 0 deletions
diff --git a/challenge-140/mohammad-anwar/perl/ch-2.pl b/challenge-140/mohammad-anwar/perl/ch-2.pl
new file mode 100644
index 0000000000..685256aec4
--- /dev/null
+++ b/challenge-140/mohammad-anwar/perl/ch-2.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+=head1
+
+Week 140:
+
+ https://theweeklychallenge.org/blog/perl-weekly-challenge-140
+
+Task #2: Multiplication Table
+
+ You are given 3 positive integers, $i, $j and $k.
+
+ Write a script to print the $kth element in the sorted multiplication table of $i and $j.
+
+=cut
+
+use Test::More;
+
+is( multiplication_table(2, 3, 4), 3, 'Example 1' );
+is( multiplication_table(3, 3, 6), 4, 'Example 2' );
+
+done_testing;
+
+sub multiplication_table {
+ my ($i, $j, $k) = @_;
+
+ die "ERROR: Missing '$i'\n." unless defined $i;
+ die "ERROR: Missing '$j'\n." unless defined $j;
+ die "ERROR: Missing '$k'\n." unless defined $k;
+
+ die "ERROR: Invalid '$i' [$i]\n." if ($i < 0);
+ die "ERROR: Invalid '$j' [$j]\n." if ($j < 0);
+ die "ERROR: Invalid '$k' [$k]\n." if ($k < 0);
+
+ my $table = [];
+ foreach my $_i (1 .. $i) {
+ foreach my $_j (1 .. $j) {
+ push @$table, $_i * $_j;
+ }
+ }
+
+ return (sort @$table)[$k-1];
+}