diff options
| author | Jaldhar H. Vyas <jaldhar@braincells.com> | 2024-01-01 15:25:44 -0500 |
|---|---|---|
| committer | Jaldhar H. Vyas <jaldhar@braincells.com> | 2024-01-01 15:25:44 -0500 |
| commit | 3c313e90e149a4bbb77c5c94921ac1cca32a569b (patch) | |
| tree | b5d5bae7c3b772bca1a4499edff86ef42707df37 | |
| parent | 994ef981f08217dba148893caf99068d9321c1d3 (diff) | |
| download | perlweeklychallenge-club-3c313e90e149a4bbb77c5c94921ac1cca32a569b.tar.gz perlweeklychallenge-club-3c313e90e149a4bbb77c5c94921ac1cca32a569b.tar.bz2 perlweeklychallenge-club-3c313e90e149a4bbb77c5c94921ac1cca32a569b.zip | |
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 |
