aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-134/sgreen/README.md4
-rwxr-xr-xchallenge-134/sgreen/perl/ch-2.pl34
2 files changed, 36 insertions, 2 deletions
diff --git a/challenge-134/sgreen/README.md b/challenge-134/sgreen/README.md
index 298af3387e..223b623bcc 100644
--- a/challenge-134/sgreen/README.md
+++ b/challenge-134/sgreen/README.md
@@ -1,3 +1,3 @@
-# The Weekly Challenge 133
+# The Weekly Challenge 134
-Solution by Simon Green. [Blog](https://dev.to/simongreennet/weekly-challenge-133-7gi)
+Solution by Simon Green.
diff --git a/challenge-134/sgreen/perl/ch-2.pl b/challenge-134/sgreen/perl/ch-2.pl
new file mode 100755
index 0000000000..e23cb45068
--- /dev/null
+++ b/challenge-134/sgreen/perl/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature 'say';
+
+sub main {
+ my ($m, $n) = @_;
+
+ # Sanity check
+ die "You must provide two values\n" unless defined $m and defined $n;
+ die "The first value is not a positive integer" unless $m =~ /^[1-9][0-9]*$/;
+ die "The second value is not a positive integer" unless $n =~ /^[1-9][0-9]*$/;
+
+ # Show the multiplication table
+ my %seen = ();
+ my $length = length($m * $n);
+ my $format = "%${length}s |" . ( " %${length}s" x $n) ."\n";
+ printf $format, 'x', 1 .. $n;
+ say '-' x ($length+1), '+', '-' x (($length+1)*$n);
+ foreach my $o (1 .. $m) {
+ my @row = map { $_ * $o } 1..$n;
+ $seen{$_} = 1 foreach @row;
+ printf $format, $o, @row;
+ }
+
+ my @numbers = sort { $a <=> $b } keys %seen;
+ say '';
+ say 'Distinct Terms: ', join ', ', @numbers;
+ say "Count: ", scalar(@numbers);
+
+}
+
+main(@ARGV);