aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-134/steven-wilson/perl/ch-2.pl34
1 files changed, 34 insertions, 0 deletions
diff --git a/challenge-134/steven-wilson/perl/ch-2.pl b/challenge-134/steven-wilson/perl/ch-2.pl
new file mode 100644
index 0000000000..a9b0b26acd
--- /dev/null
+++ b/challenge-134/steven-wilson/perl/ch-2.pl
@@ -0,0 +1,34 @@
+#!/usr/bin/env perl
+# Week 134 Task 2
+# Distinct Terms Count
+
+use strict;
+use warnings;
+use feature qw/ say /;
+use Text::ASCIITable;
+
+my ( $m, $n ) = @ARGV;
+my @rows;
+my %distict_terms;
+my $t = Text::ASCIITable->new();
+
+( defined $m && defined $n ) && ( $m =~ /^\d+$/ && $n =~ /^\d+$/ )
+ or die "2 positive integers must be passed as command line arguments\n";
+
+for my $row ( 1 .. $m ) {
+ my @multiples = map { $row * $_ } ( 1 .. $n );
+ map { $distict_terms{$_} = 1 } @multiples;
+ $rows[$row] = \@multiples;
+}
+my @distict_terms = sort { $a <=> $b } keys %distict_terms;
+
+printf( "Input: \$m = %d, \$n = %d\n", $m, $n );
+say "Output:\n";
+$t->setCols( 'x', 1 .. $n );
+$t->setOptions( { hide_FirstLine => 1, hide_LastLine => 1 } );
+for my $row ( 1 .. $m ) {
+ $t->addRow( $row, @{ $rows[$row] } );
+}
+print $t;
+say "\nDistinct Terms: ", join ", ", @distict_terms;
+say "Count: ", scalar @distict_terms;