diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-01-10 18:35:26 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-10 18:35:26 +0000 |
| commit | 8e6b8ea13f6db2802d1355c821e51706f7e8ccca (patch) | |
| tree | 4115dd59f93d2c5ddcb08d69dbc5474e042614c4 | |
| parent | a6b92825aedfd18fda122966c0de86e28acf1792 (diff) | |
| parent | 1d48c12e835eab4c2909f37f489f258e6c167663 (diff) | |
| download | perlweeklychallenge-club-8e6b8ea13f6db2802d1355c821e51706f7e8ccca.tar.gz perlweeklychallenge-club-8e6b8ea13f6db2802d1355c821e51706f7e8ccca.tar.bz2 perlweeklychallenge-club-8e6b8ea13f6db2802d1355c821e51706f7e8ccca.zip | |
Merge pull request #7391 from jacoby/master
DAJ 199
| -rw-r--r-- | challenge-199/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-199/dave-jacoby/perl/ch-1.pl | 34 | ||||
| -rw-r--r-- | challenge-199/dave-jacoby/perl/ch-2.pl | 44 |
3 files changed, 79 insertions, 0 deletions
diff --git a/challenge-199/dave-jacoby/blog.txt b/challenge-199/dave-jacoby/blog.txt new file mode 100644 index 0000000000..e790c62613 --- /dev/null +++ b/challenge-199/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2023/01/09/for-the-good-the-weekly-challenge-199.html diff --git a/challenge-199/dave-jacoby/perl/ch-1.pl b/challenge-199/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..1685992dbd --- /dev/null +++ b/challenge-199/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ fc say postderef signatures state }; + +my @examples = ( + + [ 1, 2, 3, 1, 1, 3 ], + [ 1, 2, 3 ], + [ 1, 1, 1, 1 ], + +); + +for my $e (@examples) { + my @list = $e->@*; + my $out = good_pairs(@list); + my $list = join ',', @list; + say <<"END"; + Input: \@list = ($list) + Output: $out +END +} + +sub good_pairs ( @list ) { + my $out = 0; + my $max = -1 + scalar @list; + for my $i ( 0 .. $max ) { + for my $j ( $i + 1 .. $max ) { + $out++ if $list[$i] == $list[$j]; + } + } + return $out; +} diff --git a/challenge-199/dave-jacoby/perl/ch-2.pl b/challenge-199/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..0eab4308bd --- /dev/null +++ b/challenge-199/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; +use Algorithm::Permute; + +my @examples = ( + + [ 7, 2, 3, 3, 0, 1, 1, 9, 7 ], + [ 0, 0, 1, 1, 1, 2, 2, 3 ], + +); + +for my $e (@examples) { + my $out = good_triplets( $e->@* ); + my ( $x, $y, $z, @array ) = $e->@*; + my $list = join ',', @array; + say <<"END"; + Input: \@array = ($list) and \$x = $x, \$y = $y, \$z = $z + Output: $out +END +} + +sub good_triplets ( $x, $y, $z, @array ) { + my $out = 0; + my $max = -1 + scalar @array; + for my $i ( 0 .. $max ) { + for my $j ( $i + 1 .. $max ) { + for my $k ( $j + 1 .. $max ) { + my $ij = abs( $array[$i] - $array[$j] ); + my $jk = abs( $array[$j] - $array[$k] ); + my $ik = abs( $array[$i] - $array[$k] ); + next unless $ij <= $x; + next unless $jk <= $y; + next unless $ik <= $z; + $out ++; + } + } + } + + return $out; +} + |
