aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-04-30 10:14:10 +0100
committerGitHub <noreply@github.com>2023-04-30 10:14:10 +0100
commitd0d4cd3f69cc883e5e8f93487588f489372b097d (patch)
tree9a7af6f2f5caace0ca2aaccc09401898569bb142
parentd0e2c9219d6d18ff356fa128be77779f67c91f7b (diff)
parent8fafa05f43a55c3f4e1124d5a8fc01a61083bbbd (diff)
downloadperlweeklychallenge-club-d0d4cd3f69cc883e5e8f93487588f489372b097d.tar.gz
perlweeklychallenge-club-d0d4cd3f69cc883e5e8f93487588f489372b097d.tar.bz2
perlweeklychallenge-club-d0d4cd3f69cc883e5e8f93487588f489372b097d.zip
Merge pull request #7980 from deoac/branch-for-challenge-214
Branch for challenge 214
-rw-r--r--challenge-214/shimon-ben-avraham/raku/ch-1.raku135
-rw-r--r--challenge-214/shimon-ben-avraham/raku/ch-2.raku53
2 files changed, 188 insertions, 0 deletions
diff --git a/challenge-214/shimon-ben-avraham/raku/ch-1.raku b/challenge-214/shimon-ben-avraham/raku/ch-1.raku
new file mode 100644
index 0000000000..c638bbd79e
--- /dev/null
+++ b/challenge-214/shimon-ben-avraham/raku/ch-1.raku
@@ -0,0 +1,135 @@
+#! /usr/bin/env raku
+use v6.d;
+
+#===============================================================================
+#
+# FILE: <ch-1.raku>
+#
+# DESCRIPTION: A solution to the Perl Weekly Challenge 214, Challenge 1
+#
+# AUTHOR: Shimon Bollinger (deoac.shimon@gmail.com)
+# REVISION: Last modified: Fri 28 Apr 2023 05:24:36 PM EDT
+#===============================================================================
+
+=begin comment
+ Task 1: Rank Score
+ Submitted by: Mohammad S Anwar
+ You are given a list of scores (>=1).
+
+ Write a script to rank each score in descending order. First three will
+ get medals i.e. G (Gold), S (Silver) and B (Bronze). Rest will just get
+ the ranking number.
+
+ Using the standard model of giving equal scores equal rank, then advancing
+ that number of ranks.
+
+
+ Example 1
+ Input: @scores = (1,2,4,3,5)
+ Output: (5,4,S,B,G)
+
+ Score 1 is the 5th rank.
+ Score 2 is the 4th rank.
+ Score 4 is the 2nd rank i.e. Silver (S).
+ Score 3 is the 3rd rank i.e. Bronze (B).
+ Score 5 is the 1st rank i.e. Gold (G).
+ Example 2
+ Input: @scores = (8,5,6,7,4)
+ Output: (G,4,B,S,5)
+
+ Score 8 is the 1st rank i.e. Gold (G).
+ Score 4 is the 4th rank.
+ Score 6 is the 3rd rank i.e. Bronze (B).
+ Score 7 is the 2nd rank i.e. Silver (S).
+ Score 4 is the 5th rank.
+ Example 3
+ Input: @list = (3,5,4,2)
+ Output: (B,G,S,4)
+ Example 4
+ Input: @scores = (2,5,2,1,7,5,1)
+ Output: (4,S,4,6,G,S,6)
+=end comment
+
+enum Medals « :G(1) S B »;
+my @medals = (Nil, Medals::G, Medals::S, Medals::B);
+
+subset ℕ of UInt where * > 0;
+
+#| Handle bad input
+multi sub rank-score (@scores where not (.all ~~ ℕ) --> Failure) {
+ note "All scores must be integers greater than zero!";
+ return Failure;
+} # end of multi sub rank-score (@scores where !(.all ~~ UInt) || .any ≤ 0)
+
+#| Handle good input!
+multi sub rank-score (@scores --> List) {
+ my Int $index = 0;
+ # Create a Hash where the keys are the position in the @scores array,
+ # and the values are the values of the @scores array.
+ my %index-with-scores = @scores.map: $index++ => *;
+
+ # Now create an array of pairs, reverse-sorted by value. So the winner
+ # (with the highest score) is at index 0.
+ my @index-with-scores = %index-with-scores.sort(*.values).reverse;
+
+ my Int $rank = 0;
+ my $prev-value = 0;
+ my Int $count = 1;
+ for @index-with-scores {
+ if .value == $prev-value {
+ # Handle ties
+ $count++;
+ } else {
+ # Descend the rank, usually by one
+ # (5ᵗʰ place comes after 4ᵗʰ place).
+ #
+ # But after ties, we descend more than one.
+ # (e.g. if two Gold medals are given, the next winner is in 3ʳᵈ,
+ # not 2ⁿᵈ, place and receives a Bronze medal.)
+ $rank += $count;
+ $count = 1;
+ } # end of } else
+ $prev-value = .value;
+ .value = $rank;
+ } # end of for @index-with-scores
+
+ # Now sort the array on the keys - this will return the array to its
+ # original order, but with ranks instead of scores at each index.
+ my @index-with-ranks = @index-with-scores.sort;
+
+ # We're only interested in the ranks,
+ # which are the values of each Pair in the array.
+ my @retval = @index-with-ranks.map: |*.values;
+
+ # Substitute G, S, or B for 1ˢᵗ, 2ⁿᵈ, and 3ʳᵈ place, respectively.
+ @retval .= map: { @medals[$_] // $_ };
+
+ # The challenge specified the output be a List, not an Array.
+ return @retval.List;
+} # end of sub rank-score (UInt @scores)
+
+use Test;
+
+my @test-cases = [
+ # Bad input
+ { test => &is, input => (0, 1, 2), output => Failure,
+ explanation => "Zero not allowed OK"; }
+ { test => &is, input => (1, 2.5, 3), output => Failure,
+ explanation => "Non-integers not allowed OK"; }
+ { test => &is, input => (1, 2, -3), output => Failure,
+ explanation => "Negative numbers not allowed OK"; }
+
+ # Challenge Examples
+ { test => &is-deeply, input => (1,2,4,3,5 ), output => (5,4,S,B,G ),
+ explanation => 'Example 1 OK'; }
+ { test => &is-deeply, input => (8,5,6,7,4 ), output => (G,4,B,S,5 ),
+ explanation => 'Example 2 OK'; }
+ { test => &is-deeply, input => (3,5,4,2 ), output => (B,G,S,4 ),
+ explanation => 'Example 3 OK'; }
+ { test => &is-deeply, input => (2,5,2,1,7,5,1), output => (4,S,4,6,G,S,6),
+ explanation => 'Example 4 OK'; }
+]; # end of my @test-cases
+
+for @test-cases -> %test {
+ %test<test>(rank-score(%test<input>), %test<output>, %test<explanation>);
+}
diff --git a/challenge-214/shimon-ben-avraham/raku/ch-2.raku b/challenge-214/shimon-ben-avraham/raku/ch-2.raku
new file mode 100644
index 0000000000..1f5b20126f
--- /dev/null
+++ b/challenge-214/shimon-ben-avraham/raku/ch-2.raku
@@ -0,0 +1,53 @@
+#! /usr/bin/env raku
+use v6.d;
+
+#===============================================================================
+# FiLE:
+#
+# Description:
+#
+# Author: <Shimon Bollinger> (<deoac.bollinger@gmail.com>)
+# Last Modified: Tue 25 Apr 2023 10:08:20 PM EDT
+#===============================================================================
+
+
+=begin comment
+ Task 2: Collect Points
+ Submitted by: Mohammad S Anwar
+ You are given a list of numbers.
+
+ You will perform a series of removal operations. For each operation, you remove from the list N (one or more) equal and consecutive numbers, and add to your score N × N.
+
+ Determine the maximum possible score.
+
+ Example 1:
+ Input: @numbers = (2,4,3,3,3,4,5,4,2)
+ Output: 23
+
+ We see three 3's next to each other so let us remove that first and collect 3 x 3 points.
+ So now the list is (2,4,4,5,4,2).
+ Let us now remove 5 so that all 4's can be next to each other and collect 1 x 1 point.
+ So now the list is (2,4,4,4,2).
+ Time to remove three 4's and collect 3 x 3 points.
+ Now the list is (2,2).
+ Finally remove both 2's and collect 2 x 2 points.
+ So the total points collected is 9 + 1 + 9 + 4 => 23.
+ Example 2:
+ Input: @numbers = (1,2,2,2,2,1)
+ Output: 20
+
+ Remove four 2's first and collect 4 x 4 points.
+ Now the list is (1,1).
+ Finally remove the two 1's and collect 2 x 2 points.
+ So the total points collected is 16 + 4 => 20.
+ Example 3:
+ Input: @numbers = (1)
+ Output: 1
+ Example 4:
+ Input: @numbers = (2,2,2,1,1,2,2,2)
+ Output: 40
+
+ Remove two 1's = 2 x 2 points.
+ Now the list is (2,2,2,2,2,2).
+ Then reomove six 2's = 6 x 6 points.
+=end comment