diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-05-01 03:50:55 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-01 03:50:55 +0100 |
| commit | 7f415b9dad051ab6ef7c87b04cce80848503d401 (patch) | |
| tree | 27caaed5a0edd49c226638e43582eae588c0e7aa | |
| parent | 853a00f53120d2ab04d1b7a8e4f260f8aad94483 (diff) | |
| parent | 5b218f9f44543aff4c4b8089746148b142ab8578 (diff) | |
| download | perlweeklychallenge-club-7f415b9dad051ab6ef7c87b04cce80848503d401.tar.gz perlweeklychallenge-club-7f415b9dad051ab6ef7c87b04cce80848503d401.tar.bz2 perlweeklychallenge-club-7f415b9dad051ab6ef7c87b04cce80848503d401.zip | |
Merge pull request #7995 from jaldhar/challenge-214
Challenge 214 by Jaldhar H. Vyas.
| -rw-r--r-- | challenge-214/jaldhar-h-vyas/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-214/jaldhar-h-vyas/perl/ch-1.pl | 33 | ||||
| -rwxr-xr-x | challenge-214/jaldhar-h-vyas/perl/ch-2.pl | 39 | ||||
| -rwxr-xr-x | challenge-214/jaldhar-h-vyas/raku/ch-1.raku | 33 | ||||
| -rwxr-xr-x | challenge-214/jaldhar-h-vyas/raku/ch-2.raku | 40 |
5 files changed, 146 insertions, 0 deletions
diff --git a/challenge-214/jaldhar-h-vyas/blog.txt b/challenge-214/jaldhar-h-vyas/blog.txt new file mode 100644 index 0000000000..e684437cb9 --- /dev/null +++ b/challenge-214/jaldhar-h-vyas/blog.txt @@ -0,0 +1 @@ +https://www.braincells.com/perl/2023/04/perl_weekly_challenge_week_214.html
\ No newline at end of file diff --git a/challenge-214/jaldhar-h-vyas/perl/ch-1.pl b/challenge-214/jaldhar-h-vyas/perl/ch-1.pl new file mode 100755 index 0000000000..d29e3359f8 --- /dev/null +++ b/challenge-214/jaldhar-h-vyas/perl/ch-1.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl +use 5.030; +use warnings; +use experimental qw/ switch /; + +my @scores = @ARGV; +my %quantities; +map { $quantities{$_}++ } @scores; + +my @ordered = sort { $b <=> $a } @scores; +my %ranks; +my $currentRank = 1; +my $quantity = 0; + +for my $i (0 .. scalar @ordered - 1) { + if ($quantity == 0) { + $quantity = $quantities{$ordered[$i]}; + } + $quantity--; + + given ($currentRank) { + when (1) { $ranks{$ordered[$i]} = 'G' }; + when (2) { $ranks{$ordered[$i]} = 'S' }; + when (3) { $ranks{$ordered[$i]} = 'B' }; + default { $ranks{$ordered[$i]} = $currentRank; } + } + + if ($quantity == 0) { + $currentRank += $quantities{$ordered[$i]}; + } +} + +say q{(}, ( join q{,}, map { $ranks{$_}; } @scores ), q{)}; diff --git a/challenge-214/jaldhar-h-vyas/perl/ch-2.pl b/challenge-214/jaldhar-h-vyas/perl/ch-2.pl new file mode 100755 index 0000000000..1eab60662a --- /dev/null +++ b/challenge-214/jaldhar-h-vyas/perl/ch-2.pl @@ -0,0 +1,39 @@ +#!/usr/bin/perl +use 5.030; +use warnings; + +sub findHighest { + my @numbers = @_; + my $highestScore = 0; + for my $i (0 .. scalar @numbers - 1) { + my ($points, @rest) = removeConsecutive($i, @numbers); + my $score = $points + findHighest(@rest); + if ($score > $highestScore) { + $highestScore = $score; + } + } + return $highestScore; +} + +sub removeConsecutive { + my $i = shift; + my @numbers = @_; + + my $current = $numbers[$i]; + my $quantity = 0; + + for my $n ($i .. scalar @numbers - 1) { + if ($numbers[$n] != $current) { + last; + } else { + $quantity++; + } + } + splice @numbers, $i, $quantity; + + return ($quantity * $quantity, @numbers); +} + +my @numbers = @ARGV; +say findHighest(@numbers); + diff --git a/challenge-214/jaldhar-h-vyas/raku/ch-1.raku b/challenge-214/jaldhar-h-vyas/raku/ch-1.raku new file mode 100755 index 0000000000..06ac984ab7 --- /dev/null +++ b/challenge-214/jaldhar-h-vyas/raku/ch-1.raku @@ -0,0 +1,33 @@ +#!/usr/bin/raku + +sub MAIN( + *@scores +) { + @scores.classify( { $_ }, :into(my %quantities;) ); + %quantities = %quantities.keys.map({ $_ => %quantities{$_}.elems; }); + + my @ordered = @scores.sort({ $^b <=> $^a }); + my %ranks; + my $currentRank = 1; + my $quantity = 0; + + for 0 .. @ordered.end -> $i { + if $quantity == 0 { + $quantity = %quantities{@ordered[$i]}; + } + $quantity--; + + given $currentRank { + when 1 { %ranks{@ordered[$i]} = 'G' }; + when 2 { %ranks{@ordered[$i]} = 'S' }; + when 3 { %ranks{@ordered[$i]} = 'B' }; + default { %ranks{@ordered[$i]} = $currentRank; } + } + + if $quantity == 0 { + $currentRank += %quantities{@ordered[$i]}; + } + } + + say q{(}, @scores.map({ %ranks{$_}; }).join(q{,}), q{)}; +}
\ No newline at end of file diff --git a/challenge-214/jaldhar-h-vyas/raku/ch-2.raku b/challenge-214/jaldhar-h-vyas/raku/ch-2.raku new file mode 100755 index 0000000000..b7a7e4df0e --- /dev/null +++ b/challenge-214/jaldhar-h-vyas/raku/ch-2.raku @@ -0,0 +1,40 @@ +#!/usr/bin/raku + +sub findHighest(*@numbers) { + my $highestScore = 0; + for 0 .. @numbers.end -> $i { + my $results = removeConsecutive($i, @numbers); + my $points = $results.key; + my @newNumbers = $results.value; + my $score = $points + findHighest(@newNumbers); + if ($score > $highestScore) { + $highestScore = $score; + } + } + return $highestScore; +} + +sub removeConsecutive($i, *@numbers) { + my $current = @numbers[$i]; + my $quantity = 0; + + for $i ..^ @numbers.elems -> $n { + if @numbers[$n] != $current { + last; + } else { + $quantity++; + } + } + + @numbers.splice($i, $quantity); + + return $quantity * $quantity => @numbers; +} + +sub MAIN( + *@numbers +) { + say findHighest(@numbers); +} + + |
