From be8032d23c11344c14b8ee07ab2c6d1a17fde8fc Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Fri, 15 Oct 2021 23:25:09 +0200 Subject: Add solutions to 134: Pandigital Numbers & Distinct Term Counts --- challenge-134/e-choroba/perl/ch-1.pl | 17 ++++++++++++ challenge-134/e-choroba/perl/ch-2.pl | 51 ++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100755 challenge-134/e-choroba/perl/ch-1.pl create mode 100755 challenge-134/e-choroba/perl/ch-2.pl 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); -- cgit