aboutsummaryrefslogtreecommitdiff
path: root/challenge-134/mohammad-anwar/perl/ch-2.pl
blob: 66375b4c9a871b8d57cbfc7d2381f6420747558c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/perl

use strict;
use warnings;

=head1

Week 134:

    https://theweeklychallenge.org/blog/perl-weekly-challenge-134

Task #2: Distinct Terms Count

    You are given 2 positive numbers, $m and $n.

    Write a script to generate multiplcation table and display count
    of distinct terms.

=cut

my ($table, $distinct_terms, $count_terms) = create_table(@ARGV);

print sprintf("%s\n\n", join("\n", @$table));
print sprintf("Distinct Terms: %s\n", join(", ", @$distinct_terms));
print sprintf("Count: %d\n", $count_terms);

sub create_table {
    my ($m, $n) = @_;

    die "ERROR: Missing 'm'\n." unless defined $m;
    die "ERROR: Missing 'n'\n." unless defined $n;

    die "ERROR: Invalid $m\n."  if ($m < 0);
    die "ERROR: Invalid $n\n."  if ($n < 0);

    my $table  = [];
    my $length = length($m * $n) + 1;

    my $col = 'x | 1';
    $col .= sprintf("%${length}d", $_) for (2 .. $n);
    push @$table, $col;

    my $line = '--+';
    $line .= '-' for (4 .. length($col));
    push @$table, $line;

    my %terms = ();
    $terms{$_} = 1 for (1 .. $m);

    foreach my $i (1 .. $m) {
        my $row = "$i | $i";
        foreach my $j (2 .. $n) {
            my $k = $i * $j;
            $terms{$k} = 1;
            $row .= sprintf("%${length}d", $k);
        }
        push @$table, $row;
    }

    my $distinct_terms = [ sort { $a <=> $b } keys %terms ];
    my $count_terms    = @$distinct_terms;

    return ($table, $distinct_terms, $count_terms);
}