aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-10-12 11:59:02 +0100
committerGitHub <noreply@github.com>2021-10-12 11:59:02 +0100
commit5344f83ac47a944204a7eef3fddd849215fb8814 (patch)
tree60d6d921dab3ca036902a066f93f6f9a8b52d23c
parentd1cc085e5d97d49b827aad79bd57a909caa4b0a1 (diff)
parent759e2a7ed369fad02a0da434239500d4475bcbcd (diff)
downloadperlweeklychallenge-club-5344f83ac47a944204a7eef3fddd849215fb8814.tar.gz
perlweeklychallenge-club-5344f83ac47a944204a7eef3fddd849215fb8814.tar.bz2
perlweeklychallenge-club-5344f83ac47a944204a7eef3fddd849215fb8814.zip
Merge pull request #5012 from jacoby/master
Challenge accepted
-rw-r--r--challenge-134/dave-jacoby/blog.txt1
-rw-r--r--challenge-134/dave-jacoby/node/ch-1.js58
-rw-r--r--challenge-134/dave-jacoby/perl/ch-1.pl74
-rw-r--r--challenge-134/dave-jacoby/perl/ch-2.pl74
-rw-r--r--challenge-134/dave-jacoby/python/ch-1.py59
5 files changed, 266 insertions, 0 deletions
diff --git a/challenge-134/dave-jacoby/blog.txt b/challenge-134/dave-jacoby/blog.txt
new file mode 100644
index 0000000000..796242873e
--- /dev/null
+++ b/challenge-134/dave-jacoby/blog.txt
@@ -0,0 +1 @@
+https://jacoby.github.io/2021/10/11/there-are-wrong-ways-to-skin-a-cat-the-weekly-challenge-134.html
diff --git a/challenge-134/dave-jacoby/node/ch-1.js b/challenge-134/dave-jacoby/node/ch-1.js
new file mode 100644
index 0000000000..47bb3a0cb9
--- /dev/null
+++ b/challenge-134/dave-jacoby/node/ch-1.js
@@ -0,0 +1,58 @@
+"use strict;";
+
+let output = [];
+pandigital(["1"]);
+for (i in output) {
+ console.log(["", output[i]].join("\t"));
+}
+
+function pandigital(state) {
+ let digits = {};
+ let numbers = [];
+ // we have the first five, we're done
+ if (output.length > 4) {
+ return;
+ }
+ // there was some duplicate issues, so we use
+ // indexOf to see if that value is in the array
+ // already
+ if (state.length == 10) {
+ let pandigit = state.join("");
+ if (output.indexOf(pandigit) === -1) {
+ output.push(pandigit);
+ return;
+ }
+ }
+ // I think I would normally prefer if
+ // for ( i in array ) would give me the value
+ // within the array not the index, but there
+ // are enough times that I want just that, so
+ // I don't need that change
+ for (let i in state) {
+ let n = state[i];
+ digits[n] = 1;
+ }
+ // the long way around getting a range
+ let range = Array(10)
+ .fill()
+ .map((n, i) => i);
+ // yes, I could've probably written a filter,
+ // but this is understandable
+ for (let i in range) {
+ if (digits[i] === undefined) {
+ numbers.push(i);
+ }
+ }
+ // the duplicates issue was related to
+ // losing track of the parens, so that
+ // this loop was inside the range loop
+ // above, but it works and I've featured
+ // the cool indexOf method, so I'll let
+ // the belt-and-suspenders solution stand.
+ for (let i in numbers) {
+ let n = numbers[i];
+ let newstate = [...state];
+ newstate.push(n);
+ pandigital(newstate);
+ }
+}
diff --git a/challenge-134/dave-jacoby/perl/ch-1.pl b/challenge-134/dave-jacoby/perl/ch-1.pl
new file mode 100644
index 0000000000..77657816bd
--- /dev/null
+++ b/challenge-134/dave-jacoby/perl/ch-1.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say state postderef signatures };
+no warnings qw{ experimental };
+
+use Algorithm::Permute;
+
+my @x = pandigital_1();
+my @y = pandigital_2();
+my @z = pandigital_3();
+
+my @headers = qw{I PANDIGITAL1 PANDIGITAL2 PANDIGITAL3};
+say join "\t", @headers;
+say join "\t", map {s/./-/gmix;$_} @headers;
+for my $i ( 0 .. 4 ) {
+ say join "\t", $i, $x[$i], $y[$i], $z[$i];
+}
+
+
+sub pandigital_1 {
+ my @output;
+ my @nums = ( 0, 2 .. 9 );
+ my $p = Algorithm::Permute->new( \@nums );
+ while ( my @res = $p->next ) {
+ my $n = join '', 1, @res;
+ push @output, $n;
+ }
+ @output = sort { $a <=> $b } @output;
+ return @output[ 0 .. 4 ];
+}
+
+sub pandigital_2 {
+ my $output = [];
+ my $state = [1];
+ _pandigital_2( $output, $state );
+ my @output = $output->@*;
+ return @output[ 0 .. 4 ];
+}
+
+sub _pandigital_2 ( $output, $state ) {
+ my %state = map { $_ => 1 } $state->@*;
+ my @digits = grep { !$state{$_} } 0 .. 9;
+ if ( scalar $output->@* > 5 ) { return }
+ if ( scalar $state->@* == 10 ) {
+ my $pandigit = join '', $state->@*;
+ push $output->@*, $pandigit;
+ return;
+ }
+ for my $i (@digits) {
+ my $newstate->@* = $state->@*;
+ push $newstate->@*, $i;
+ _pandigital_2( $output, $newstate );
+ }
+ return;
+}
+
+sub pandigital_3 {
+ my @output;
+ my $i = 1023456789;
+ while ( scalar @output < 5 ) {
+ push @output, $i if is_pandigital($i);
+ $i++;
+ }
+ return @output[ 0 .. 4 ];
+}
+
+sub is_pandigital ( $n ) {
+ for my $i ( 0 .. 9 ) {
+ return 0 unless $n =~ /$i/;
+ }
+ return 1;
+}
diff --git a/challenge-134/dave-jacoby/perl/ch-2.pl b/challenge-134/dave-jacoby/perl/ch-2.pl
new file mode 100644
index 0000000000..4bec861482
--- /dev/null
+++ b/challenge-134/dave-jacoby/perl/ch-2.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use feature qw{ say postderef signatures };
+no warnings qw{ experimental };
+
+use Carp;
+use Getopt::Long;
+use List::Util qw{ uniq };
+
+my $x = 3;
+my $y = $x;
+
+GetOptions(
+ 'x=i' => \$x,
+ 'y=i' => \$y,
+);
+
+croak 'X not positive' unless $x > 0;
+croak 'Y not positive' unless $x > 0;
+croak 'X not integer' unless $x == int $x;
+croak 'Y not integer' unless $y == int $y;
+
+make_table( $x, $y );
+
+sub make_table ( $x, $y ) {
+ my @headers = make_line( 'x', '|', 1 .. $y );
+ my $headers = join ' ', @headers;
+ my $line = $headers;
+ $line =~ s/\|/+/gmix;
+ $line =~ s/[\w\s]/-/gmix;
+
+ say qq{\$x = $x , \$y = $y };
+ say '';
+ say $headers;
+ say $line;
+ my $matrix = make_matrix( $x, $y );
+ my @dt = uniq sort {$a<=>$b} flatten_matrix($matrix);
+ my $dt = join ', ', @dt;
+ my $count = scalar @dt;
+
+ my $c = 0;
+ for my $i ( $matrix->@* ) {
+ $c++;
+ my $line = make_line( $c, '|', $i->@* );
+ say $line;
+ }
+ say '';
+ say qq{Distinct Terms: $dt};
+ say qq{Count: $count};
+}
+
+sub make_line ( @array ) {
+ my @headers = ( map { sprintf '%3s', $_ } @array );
+ return join ' ', @headers;
+}
+
+sub make_matrix ( $x, $y ) {
+ my $matrix;
+ for my $i ( 0 .. $x - 1 ) {
+ my $ii = $i + 1;
+ for my $j ( 0 .. $y - 1 ) {
+ my $jj = $j + 1;
+ my $tt = $ii * $jj;
+ $matrix->[$i][$j] = $tt;
+ }
+ }
+ return $matrix;
+}
+
+sub flatten_matrix ( $matrix ) {
+ return map { $_->@* } $matrix->@*;
+} \ No newline at end of file
diff --git a/challenge-134/dave-jacoby/python/ch-1.py b/challenge-134/dave-jacoby/python/ch-1.py
new file mode 100644
index 0000000000..9dbfd418f0
--- /dev/null
+++ b/challenge-134/dave-jacoby/python/ch-1.py
@@ -0,0 +1,59 @@
+#!/usr/bin/env python3
+
+# would want to pass an array reference, but
+# haven't found how you can do that in Python.
+# It might not be possible.
+output=[]
+
+def main():
+ pandigital()
+
+def pandigital():
+ # because we see 0123 as 123, we cannot start
+ # with 0, so we force the issue by starting with
+ # one
+ state=["1"]
+ _pandigital(state)
+ for o in output:
+ print( o )
+
+def _pandigital(state):
+ # we're dumping to a global array
+ # so we can know when we've hit five
+ # and can just bail
+ if len(output)>4:
+ return
+ # if length is 10, we've used all the
+ # digits and can convert the array of
+ # digits into a number and append that
+ # to output
+ if len(state)==10:
+ pandigital = int("".join(list(state)))
+ output.append(pandigital)
+ return
+ # dicts are like hashes and useful to
+ # keep you from using the same thing twice
+ # (asterisk)
+ mydict = {}
+ numbers = []
+ # thing is, join wants a string, so we have
+ # to always treat the digits as strings, and
+ # so we must cast to string before adding to
+ # the dictionary
+ for n in state:
+ mydict[str(n)] = 1
+ # and also here, so we know that once we've
+ # used 0, we don't use it again
+ for n in range(10):
+ if str(n) not in mydict:
+ numbers.append(str(n))
+ # and here, we copy the current state array,
+ # append what numbers we have, and recurse
+ for n in numbers:
+ newstate = state.copy()
+ newstate.append(n)
+ _pandigital(newstate)
+ return
+
+if __name__ == '__main__':
+ main() \ No newline at end of file