diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-11-23 10:49:31 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-11-23 10:49:31 +0000 |
| commit | d931ba7fbb2d57ca1e5123ea29b80f1c0dbc28e5 (patch) | |
| tree | 17742a876110bafb86e25146aa90694de376bf60 /challenge-244 | |
| parent | 62bbc3198640c281e4e692647fca6f18b4f53b60 (diff) | |
| download | perlweeklychallenge-club-d931ba7fbb2d57ca1e5123ea29b80f1c0dbc28e5.tar.gz perlweeklychallenge-club-d931ba7fbb2d57ca1e5123ea29b80f1c0dbc28e5.tar.bz2 perlweeklychallenge-club-d931ba7fbb2d57ca1e5123ea29b80f1c0dbc28e5.zip | |
- Added solutions by Ali Moradi.
- Added solutions by Laurent Rosenfeld.
Diffstat (limited to 'challenge-244')
| -rw-r--r-- | challenge-244/laurent-rosenfeld/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-244/laurent-rosenfeld/perl/ch-2.pl | 25 | ||||
| -rw-r--r-- | challenge-244/laurent-rosenfeld/raku/ch-2.raku | 12 |
3 files changed, 38 insertions, 0 deletions
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; +} |
