aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-10-15 23:55:23 +0100
committerGitHub <noreply@github.com>2021-10-15 23:55:23 +0100
commit11e96206bd67cd2730dc182635b666194e3766c0 (patch)
tree90f0fce8f6c34525d81667e8a3eb17139fec759f
parentc08bf5cc881a08c7be39cdeb5443474c4fd64621 (diff)
parentbe8032d23c11344c14b8ee07ab2c6d1a17fde8fc (diff)
downloadperlweeklychallenge-club-11e96206bd67cd2730dc182635b666194e3766c0.tar.gz
perlweeklychallenge-club-11e96206bd67cd2730dc182635b666194e3766c0.tar.bz2
perlweeklychallenge-club-11e96206bd67cd2730dc182635b666194e3766c0.zip
Merge pull request #5029 from choroba/ech134
Add solutions to 134: Pandigital Numbers & Distinct Term Counts
-rwxr-xr-xchallenge-134/e-choroba/perl/ch-1.pl17
-rwxr-xr-xchallenge-134/e-choroba/perl/ch-2.pl51
2 files changed, 68 insertions, 0 deletions
diff --git a/challenge-134/e-choroba/perl/ch-1.pl b/challenge-134/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..1fbc58bced
--- /dev/null
+++ b/challenge-134/e-choroba/perl/ch-1.pl
@@ -0,0 +1,17 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use feature qw{ say };
+
+sub pandigital_numbers {
+ my ($count) = @_;
+ my @pandigitals = (1023456789);
+ while (--$count) {
+ my $next = 1 + $pandigitals[-1];
+ ++$next until 10 == grep -1 != index($next, $_), 0 .. 9;
+ push @pandigitals, $next;
+ }
+ say for @pandigitals;
+}
+
+pandigital_numbers(10);
diff --git a/challenge-134/e-choroba/perl/ch-2.pl b/challenge-134/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..3faae6c83e
--- /dev/null
+++ b/challenge-134/e-choroba/perl/ch-2.pl
@@ -0,0 +1,51 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use feature qw{ say };
+
+sub populate {
+ my ($rows, $cols) = @_;
+ my @table;
+ for my $x (1 .. $rows) {
+ for my $y (1 .. $cols) {
+ $table[ $x - 1 ][ $y - 1 ] = $x * $y;
+ }
+ }
+ return \@table
+}
+
+sub header_line {
+ my ($cols, $width, $header_width) = @_;
+ printf "%${header_width}s |", 'x';
+ print join ' ', map sprintf("%${width}d", $_), 1 .. $cols;
+ print "\n";
+
+ print '-' x $header_width, '-+';
+ print '-' x $width;
+ print '-' x $width, '-' for 2 .. $cols;
+ print "\n";
+
+}
+
+sub distinct_term_counts {
+ my ($rows, $cols) = @_;
+ my $table = populate($rows, $cols);
+ my $width = length $table->[-1][-1];
+ my $header_width = length $rows;
+
+ header_line($cols, $width, $header_width);
+
+ my %distinct;
+ for my $x (0 .. $rows - 1) {
+ printf "%${header_width}d |", $x + 1;
+ print join ' ', map {
+ undef $distinct{ $table->[$x][$_] };
+ sprintf "%${width}d", $table->[$x][$_];
+ } 0 .. $cols - 1;
+ print "\n";
+ }
+ say 'Distinct terms: ', join ', ', sort { $a <=> $b } keys %distinct;
+ say 'Count: ', scalar keys %distinct;
+}
+
+distinct_term_counts(3, 5);