aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2023-11-21 10:44:53 +0000
committerGitHub <noreply@github.com>2023-11-21 10:44:53 +0000
commita5bd27987ca7a89b2aca687800ef17bfeba7d3bb (patch)
tree13448128284b37f3d9cacc383ddaffe49442a252
parent183a28ffae02f5d1dcc92435a9f340f3f4964db1 (diff)
parentbdcec422dc6c7f2bd8b60e1f69d1ac9bf46c4fc8 (diff)
downloadperlweeklychallenge-club-a5bd27987ca7a89b2aca687800ef17bfeba7d3bb.tar.gz
perlweeklychallenge-club-a5bd27987ca7a89b2aca687800ef17bfeba7d3bb.tar.bz2
perlweeklychallenge-club-a5bd27987ca7a89b2aca687800ef17bfeba7d3bb.zip
Merge pull request #9111 from jacoby/master
Blogged 244
-rw-r--r--challenge-244/dave-jacoby/blog.txt1
-rw-r--r--challenge-244/dave-jacoby/perl/ch-1.pl34
-rw-r--r--challenge-244/dave-jacoby/perl/ch-2.pl37
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;
+}