diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-07 23:47:58 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-07 23:47:58 +0000 |
| commit | 8d699603635f1a47af9fd27a178d19719163e557 (patch) | |
| tree | 26832db2018c4096d25d6b6cb45bd55a23eab635 | |
| parent | 1aaae4c7c17a6a48454ff7c6edeb77727990d7e0 (diff) | |
| parent | 3c313e90e149a4bbb77c5c94921ac1cca32a569b (diff) | |
| download | perlweeklychallenge-club-8d699603635f1a47af9fd27a178d19719163e557.tar.gz perlweeklychallenge-club-8d699603635f1a47af9fd27a178d19719163e557.tar.bz2 perlweeklychallenge-club-8d699603635f1a47af9fd27a178d19719163e557.zip | |
Merge pull request #9361 from jaldhar/challenge-245
Challenge 245 by Jaldhar H. Vyas.
| -rw-r--r-- | challenge-245/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-245/jaldhar-h-vyas/perl/ch-1.pl | 22 | ||||
| -rwxr-xr-x | challenge-245/jaldhar-h-vyas/perl/ch-2.pl | 53 | ||||
| -rwxr-xr-x | challenge-245/jaldhar-h-vyas/raku/ch-1.raku | 17 | ||||
| -rwxr-xr-x | challenge-245/jaldhar-h-vyas/raku/ch-2.raku | 17 |
5 files changed, 110 insertions, 0 deletions
diff --git a/challenge-245/jaldhar-h-vyas/blog.txt b/challenge-245/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..4c5e93ba5a --- /dev/null +++ b/challenge-245/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2024/01/perl_weekly_challenge_week_245.html diff --git a/challenge-245/jaldhar-h-vyas/perl/ch-1.pl b/challenge-245/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..e4dbe17650 --- /dev/null +++ b/challenge-245/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/perl +use 5.030; +use warnings; + +my @lang1 = qw/ perl c python /; +my @popularity1 = qw/ 2 1 3 /; + +my @lang2 = qw/ c++ haskell java /; +my @popularity2 = qw/ 1 3 2 /; + +sub sortLanguages { + my ($lang, $popularity) = @_; + my %ranking; + for my $i (0 .. scalar @{$lang} - 1) { + $ranking{$lang->[$i]} = $popularity->[$i]; + } + + return sort { $ranking{$a} <=> $ranking{$b}} keys %ranking; +} + +say join q{, }, sortLanguages(\@lang1, \@popularity1); +say join q{, }, sortLanguages(\@lang2, \@popularity2); diff --git a/challenge-245/jaldhar-h-vyas/perl/ch-2.pl b/challenge-245/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..7f391af734 --- /dev/null +++ b/challenge-245/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,53 @@ +#!/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; +} + +sub permute (&@) { + my $code = shift; + my @idx = 0..$#_; + while ( $code->(@_[@idx]) ) { + my $p = $#idx; + --$p while $idx[$p-1] > $idx[$p]; + my $q = $p or return; + push @idx, reverse splice @idx, $p; + ++$q while $idx[$p-1] > $idx[$q]; + @idx[$p-1,$q]=@idx[$q,$p-1]; + } +} + +my $max = -1; + +for my $elems (3 .. (scalar @ARGV)) { + for my $combo (combinations(\@ARGV, $elems)) { + my @permutations; + permute { push @permutations, \@_; } @{$combo}; + for my $perm (grep { $_ % 3 == 0 } map { join q{}, @{$_} } @permutations) { + if ($perm > $max) { + $max = $perm; + } + } + } +} + +say $max; diff --git a/challenge-245/jaldhar-h-vyas/raku/ch-1.raku b/challenge-245/jaldhar-h-vyas/raku/ch-1.raku new file mode 100755 index 0000000000..7148b6c357 --- /dev/null +++ b/challenge-245/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,17 @@ +#!/usr/bin/raku + +my @lang1 = <perl c python>; +my @popularity1 = <2 1 3>; + +my @lang2 = <c++ haskell java>; +my @popularity2 = <1 3 2>; + +sub sortLanguages(@lang, @popularity) { + my %ranking = @lang Z=> @popularity; + return %ranking.keys.sort({ %ranking{$^a} <=> %ranking{$^b}}); +} + +sub MAIN() { + sortLanguages(@lang1, @popularity1).join(q{, }).say; + sortLanguages(@lang2, @popularity2).join(q{, }).say; +}
\ No newline at end of file diff --git a/challenge-245/jaldhar-h-vyas/raku/ch-2.raku b/challenge-245/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..bc62a0610f --- /dev/null +++ b/challenge-245/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,17 @@ +#!/usr/bin/raku + +sub MAIN( + *@ints +) { + my $max = -1; + + for @ints.combinations(3 .. @ints.elems) -> $combo { + for @$combo.permutations().map({ $_.join }).grep({ $_ %% 3 }) -> $perm { + if $perm > $max { + $max = $perm; + } + } + } + + say $max; +}
\ No newline at end of file |
