diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-02-26 10:02:15 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-02-26 10:02:15 +0000 |
| commit | db10839eaed516a12c765891afe0e5694c18a6bb (patch) | |
| tree | 6fc050d9d755fa2cc0ebad16d15522f9bc3a5647 | |
| parent | a57746910933910e8651fa220a7b7a8989e552c5 (diff) | |
| parent | d733892add6154681a779f06231fe9295b90e31d (diff) | |
| download | perlweeklychallenge-club-db10839eaed516a12c765891afe0e5694c18a6bb.tar.gz perlweeklychallenge-club-db10839eaed516a12c765891afe0e5694c18a6bb.tar.bz2 perlweeklychallenge-club-db10839eaed516a12c765891afe0e5694c18a6bb.zip | |
Merge pull request #7632 from jaldhar/challenge-205
Challenge 205 by Jaldhar H. Vyas.
| -rw-r--r-- | challenge-205/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-205/jaldhar-h-vyas/perl/ch-1.pl | 17 | ||||
| -rwxr-xr-x | challenge-205/jaldhar-h-vyas/perl/ch-2.pl | 26 | ||||
| -rwxr-xr-x | challenge-205/jaldhar-h-vyas/raku/ch-1.raku | 8 | ||||
| -rwxr-xr-x | challenge-205/jaldhar-h-vyas/raku/ch-2.sh | 3 |
5 files changed, 55 insertions, 0 deletions
diff --git a/challenge-205/jaldhar-h-vyas/blog.txt b/challenge-205/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..aef4d5c575 --- /dev/null +++ b/challenge-205/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2023/02/perl_weekly_challenge_week_205.html
\ No newline at end of file diff --git a/challenge-205/jaldhar-h-vyas/perl/ch-1.pl b/challenge-205/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..79923ea3f8 --- /dev/null +++ b/challenge-205/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl +use 5.030; +use warnings; + +sub unique { + my (@array) = @{ $_[0] }; + my %unique; + for my $elem (@array) { + $unique{$elem}++; + } + + return keys %unique; +} + +my @array = sort { $b <=> $a } unique(\@ARGV); + +say scalar @array > 2 ? $array[2] : $array[0]; diff --git a/challenge-205/jaldhar-h-vyas/perl/ch-2.pl b/challenge-205/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..2b72532e2f --- /dev/null +++ b/challenge-205/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl +use 5.030; +use warnings; + +sub combinations { + my @list = @{$_[0]}; + my $length = $_[1]; + + if ($length <= 1) { + return map [$_], @list; + } + + my @combos; + + for (my $i = 0; $i + $length <= scalar @list; $i++) { + my $val = $list[$i]; + my @rest = @list[$i + 1 .. $#list]; + for my $c (combinations(\@rest, $length - 1)) { + push @combos, [$val, @{$c}] ; + } + } + + return @combos; +} + +say 0+(sort { $b <=> $a } map { $_->[0] ^ $_->[1] } combinations(\@ARGV, 2))[0]; diff --git a/challenge-205/jaldhar-h-vyas/raku/ch-1.raku b/challenge-205/jaldhar-h-vyas/raku/ch-1.raku new file mode 100755 index 0000000000..b658f00a1d --- /dev/null +++ b/challenge-205/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,8 @@ +#!/usr/bin/raku + +sub MAIN( + *@args +) { + my @array = @args.sort({ $^b <=> $^a }).unique; + say @array.elems > 2 ?? @array[2] !! @array[0]; +}
\ No newline at end of file diff --git a/challenge-205/jaldhar-h-vyas/raku/ch-2.sh b/challenge-205/jaldhar-h-vyas/raku/ch-2.sh new file mode 100755 index 0000000000..127afd8533 --- /dev/null +++ b/challenge-205/jaldhar-h-vyas/raku/ch-2.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +raku -e '@*ARGS.combinations(2).map({ @$_[0] +^ @$_[1] }).sort({ $^b <=> $^a }).first.say;' $@ |
