diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2021-10-11 18:16:35 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2021-10-16 11:19:11 +0200 |
| commit | 1b002a641db1ceef820fe28b8a1df54983e815b7 (patch) | |
| tree | d4a9c5e0041273512a065cbcf8d3bd0e42b5c6a0 | |
| parent | 714cdbfbaf821a26838df4d215bf5428333f89fa (diff) | |
| download | perlweeklychallenge-club-1b002a641db1ceef820fe28b8a1df54983e815b7.tar.gz perlweeklychallenge-club-1b002a641db1ceef820fe28b8a1df54983e815b7.tar.bz2 perlweeklychallenge-club-1b002a641db1ceef820fe28b8a1df54983e815b7.zip | |
Solution to task 2
| -rwxr-xr-x | challenge-134/jo-37/perl/ch-2.pl | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/challenge-134/jo-37/perl/ch-2.pl b/challenge-134/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..d3ab47ea29 --- /dev/null +++ b/challenge-134/jo-37/perl/ch-2.pl @@ -0,0 +1,54 @@ +#!/usr/bin/perl -s + +use v5.16; +use PDL; +use Test2::V0 '!float'; +use experimental 'signatures'; + +our $examples; + +run_tests() if $examples; # does not return + +die <<EOS unless @ARGV == 2; +usage: $0 [-examples] [M N] + +-examples + run the examples from the challenge + +M N + count distinct elements in multiplication table for M and N + +EOS + + +### Input and Output + +say num_dist_terms(@ARGV[0, 1]); + + +### Implementation + +# Calculate the multiplication table as the outer product of two +# sequences starting with 1, take the unique values thereof and count +# these. +# The task description together with its examples is ambiguous: +# "generate table and display count" I'd take as just to print the +# count. OTOH, the examples provide the multiplication table, the +# distinct terms and the count in the OUTPUT section. Lazily following +# the task description. + +sub num_dist_terms ($m, $n) { + outer(sequence(long, $m) + 1, sequence(long, $n) + 1)->uniq->dim(0); +} + + +### Examples and tests + +sub run_tests { + + is num_dist_terms(3, 3), 6, 'example 1'; + is num_dist_terms(3, 5), 11, 'example 2'; + + done_testing; + exit; +} |
