From 371a7c6db7e3b1a1d011185ad195f4950054a0a1 Mon Sep 17 00:00:00 2001 From: deoac Date: Tue, 25 Apr 2023 22:10:30 -0400 Subject: Initial skeleton files --- challenge-214/shimon-ben-avraham/raku/ch-1.raku | 48 ++++++++++++++++++++++ challenge-214/shimon-ben-avraham/raku/ch-2.raku | 53 +++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 challenge-214/shimon-ben-avraham/raku/ch-1.raku create mode 100644 challenge-214/shimon-ben-avraham/raku/ch-2.raku 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..ae79f4641c --- /dev/null +++ b/challenge-214/shimon-ben-avraham/raku/ch-1.raku @@ -0,0 +1,48 @@ +#! /usr/bin/env raku +use v6.d; + +#=============================================================================== +# +# FILE: <.raku> +# +# DESCRIPTION: +# +# AUTHOR: Shimon Bollinger (deoac.shimon@gmail.com) +# REVISION: Last modified: Tue 25 Apr 2023 10:08: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 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: () +# 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 -- cgit From 2e0d9ef321414dc8c66011139423252786c533ae Mon Sep 17 00:00:00 2001 From: deoac Date: Fri, 28 Apr 2023 16:30:43 -0400 Subject: Submission 1 --- challenge-214/shimon-ben-avraham/raku/ch-1.raku | 82 +++++++++++++++++++++++-- 1 file changed, 77 insertions(+), 5 deletions(-) diff --git a/challenge-214/shimon-ben-avraham/raku/ch-1.raku b/challenge-214/shimon-ben-avraham/raku/ch-1.raku index ae79f4641c..7f814c0b19 100644 --- a/challenge-214/shimon-ben-avraham/raku/ch-1.raku +++ b/challenge-214/shimon-ben-avraham/raku/ch-1.raku @@ -3,12 +3,12 @@ use v6.d; #=============================================================================== # -# FILE: <.raku> +# FILE: # -# DESCRIPTION: +# DESCRIPTION: A solution to the Perl Weekly Challenge 214, Challenge 1 # # AUTHOR: Shimon Bollinger (deoac.shimon@gmail.com) -# REVISION: Last modified: Tue 25 Apr 2023 10:08:36 PM EDT +# REVISION: Last modified: Fri 28 Apr 2023 04:28:06 PM EDT #=============================================================================== =begin comment @@ -16,9 +16,12 @@ use v6.d; 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. + 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. + Using the standard model of giving equal scores equal rank, then advancing + that number of ranks. Example 1 @@ -46,3 +49,72 @@ use v6.d; 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 ~~ ℕ) ) { + note "All scores must be integers greater than zero!"; + return Nil; +} # end of multi sub rank-score (@scores where !(.all ~~ UInt) || .any ≤ 0) + +#| Handle good input! +multi sub rank-score (@scores) { + 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; + +is rank-score((0, 1, 2)), Nil, "Zero not allowed OK"; +is rank-score((1, 2.5, 3)), Nil, "Non-integers not allowed OK"; +is rank-score((1, 2, -3)), Nil, "Negative numbers not allowed OK"; + +is-deeply rank-score((1,2,4,3,5)), (5,4,S,B,G), 'Example 1 OK'; +is-deeply rank-score((8,5,6,7,4)), (G,4,B,S,5), 'Example 2 OK'; +is-deeply rank-score((3,5,4,2)), (B,G,S,4), 'Example 3 OK'; +is-deeply rank-score((2,5,2,1,7,5,1)), (4,S,4,6,G,S,6), 'Example 4 OK'; -- cgit From 8fafa05f43a55c3f4e1124d5a8fc01a61083bbbd Mon Sep 17 00:00:00 2001 From: deoac Date: Fri, 28 Apr 2023 17:31:28 -0400 Subject: Improved testing --- challenge-214/shimon-ben-avraham/raku/ch-1.raku | 39 +++++++++++++++++-------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/challenge-214/shimon-ben-avraham/raku/ch-1.raku b/challenge-214/shimon-ben-avraham/raku/ch-1.raku index 7f814c0b19..c638bbd79e 100644 --- a/challenge-214/shimon-ben-avraham/raku/ch-1.raku +++ b/challenge-214/shimon-ben-avraham/raku/ch-1.raku @@ -8,7 +8,7 @@ use v6.d; # 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 04:28:06 PM EDT +# REVISION: Last modified: Fri 28 Apr 2023 05:24:36 PM EDT #=============================================================================== =begin comment @@ -56,13 +56,13 @@ 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 ~~ ℕ) ) { +multi sub rank-score (@scores where not (.all ~~ ℕ) --> Failure) { note "All scores must be integers greater than zero!"; - return Nil; + return Failure; } # end of multi sub rank-score (@scores where !(.all ~~ UInt) || .any ≤ 0) #| Handle good input! -multi sub rank-score (@scores) { +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. @@ -110,11 +110,26 @@ multi sub rank-score (@scores) { use Test; -is rank-score((0, 1, 2)), Nil, "Zero not allowed OK"; -is rank-score((1, 2.5, 3)), Nil, "Non-integers not allowed OK"; -is rank-score((1, 2, -3)), Nil, "Negative numbers not allowed OK"; - -is-deeply rank-score((1,2,4,3,5)), (5,4,S,B,G), 'Example 1 OK'; -is-deeply rank-score((8,5,6,7,4)), (G,4,B,S,5), 'Example 2 OK'; -is-deeply rank-score((3,5,4,2)), (B,G,S,4), 'Example 3 OK'; -is-deeply rank-score((2,5,2,1,7,5,1)), (4,S,4,6,G,S,6), 'Example 4 OK'; +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(rank-score(%test), %test, %test); +} -- cgit