diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2023-11-20 20:20:57 -0500 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2023-11-20 20:20:57 -0500 |
| commit | bdcec422dc6c7f2bd8b60e1f69d1ac9bf46c4fc8 (patch) | |
| tree | 1f7758ebf83697eb9c1340c4d9fac4fb994127db | |
| parent | d3c171ecb6715a7bd21a52fab583c4171605f5c0 (diff) | |
| download | perlweeklychallenge-club-bdcec422dc6c7f2bd8b60e1f69d1ac9bf46c4fc8.tar.gz perlweeklychallenge-club-bdcec422dc6c7f2bd8b60e1f69d1ac9bf46c4fc8.tar.bz2 perlweeklychallenge-club-bdcec422dc6c7f2bd8b60e1f69d1ac9bf46c4fc8.zip | |
Blogged 244
| -rw-r--r-- | challenge-244/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-244/dave-jacoby/perl/ch-1.pl | 34 | ||||
| -rw-r--r-- | challenge-244/dave-jacoby/perl/ch-2.pl | 37 |
3 files changed, 72 insertions, 0 deletions
diff --git a/challenge-244/dave-jacoby/blog.txt b/challenge-244/dave-jacoby/blog.txt new file mode 100644 index 0000000000..8ef1453319 --- /dev/null +++ b/challenge-244/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2023/11/20/we-can-be-heroes-just-for-one-day-weekly-challenge-244.html diff --git a/challenge-244/dave-jacoby/perl/ch-1.pl b/challenge-244/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..c9990cbdb2 --- /dev/null +++ b/challenge-244/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Compare; + +my @examples = ( + + [ 8, 1, 2, 2, 3 ], + [ 6, 5, 4, 8 ], + [ 2, 2, 2 ], +); +for my $e (@examples) { + my @output = count_smaller( $e->@* ); + my $output = join ', ', @output; + my $input = join ', ', $e->@*; + say <<~"END"; + Input: \@int = ($input) + Output: ($output) + END +} + +sub count_smaller (@input) { + my @output; + for my $v (@input) { + # first pass, I copied the array to remove the current value + # but once I realized that $v is never going to be less than + # itself, I decided to make it simpler. + push @output, scalar grep { $_ < $v } @input; + } + return @output; +} diff --git a/challenge-244/dave-jacoby/perl/ch-2.pl b/challenge-244/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..3f44f50d9a --- /dev/null +++ b/challenge-244/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,37 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ min max }; +use Math::Combinatorics; + +my @examples = ( + + [ 2, 1, 4 ], +); + +for my $e (@examples) { + my $input = join ', ', $e->@*; + my $output = group_hero( $e->@* ); + + say <<~"END"; + Input: \$input = ($input) + Output: $output + END +} + +sub group_hero (@input) { + my $output = 0; + for my $c ( 1 .. scalar @input ) { + my $comb = Math::Combinatorics->new( count => $c, data => [@input], ); + while ( my @combo = $comb->next_combination ) { + my $min = min @combo; + my $max = max @combo; + my $str = $max**2 * $min; + $output += $str; + } + } + return $output; +} |
