From d931ba7fbb2d57ca1e5123ea29b80f1c0dbc28e5 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Thu, 23 Nov 2023 10:49:31 +0000 Subject: - Added solutions by Ali Moradi. - Added solutions by Laurent Rosenfeld. --- challenge-244/laurent-rosenfeld/blog1.txt | 1 + challenge-244/laurent-rosenfeld/perl/ch-2.pl | 25 +++++++++++++++++++++++++ challenge-244/laurent-rosenfeld/raku/ch-2.raku | 12 ++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 challenge-244/laurent-rosenfeld/blog1.txt create mode 100644 challenge-244/laurent-rosenfeld/perl/ch-2.pl create mode 100644 challenge-244/laurent-rosenfeld/raku/ch-2.raku (limited to 'challenge-244') diff --git a/challenge-244/laurent-rosenfeld/blog1.txt b/challenge-244/laurent-rosenfeld/blog1.txt new file mode 100644 index 0000000000..109bfe8826 --- /dev/null +++ b/challenge-244/laurent-rosenfeld/blog1.txt @@ -0,0 +1 @@ +https://blogs.perl.org/users/laurent_r/2023/11/perl-weekly-challenge-244-group-hero.html diff --git a/challenge-244/laurent-rosenfeld/perl/ch-2.pl b/challenge-244/laurent-rosenfeld/perl/ch-2.pl new file mode 100644 index 0000000000..a1b6637138 --- /dev/null +++ b/challenge-244/laurent-rosenfeld/perl/ch-2.pl @@ -0,0 +1,25 @@ +use strict; +use warnings; +use feature 'say'; + +sub group_hero { + # Caution: this works only with input arrays of 3 items + my @in = sort { $a <=> $b } @_; + my $sum = 0; + $sum += $_ * $_ * $_ for @in; + my ($i, $j) = (0, 0); + + for $j (0..$#in) { + for $i (1..$#in) { #gap + next unless defined $in[$j + $i]; + $sum += $in[$j] * $in[$j + $i] * $in[$j + $i]; + } + } + $sum += $in[0] * $in[$#in] * $in[$#in]; + return $sum; +} + +for my $test ([<2 1 4>]) { + printf "%-10s => ", "@$test"; + say group_hero @$test; +} diff --git a/challenge-244/laurent-rosenfeld/raku/ch-2.raku b/challenge-244/laurent-rosenfeld/raku/ch-2.raku new file mode 100644 index 0000000000..88f105cd5e --- /dev/null +++ b/challenge-244/laurent-rosenfeld/raku/ch-2.raku @@ -0,0 +1,12 @@ +sub group-hero (@in) { + my $sum = 0; + for @in.combinations: 1..@in.elems -> @comb { + $sum += @comb.max² * @comb.min; + } + return $sum; +} + +for <2 1 4>, <4 1 5 2> -> @test { + printf "%-10s => ", "@test[]"; + say group-hero @test; +} -- cgit