diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2023-05-21 19:25:16 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-21 19:25:16 +0100 |
| commit | 949b5020dc980a81bf593d19be3e6f3258d8a4c9 (patch) | |
| tree | 2761295c12c525bdbc3efedf4cce843f11b09024 | |
| parent | 6f09db07a740c87bd1b0af7fe1a581f69d429124 (diff) | |
| parent | 0ec38a2edfb17fb341a769deab142cebefcdbc0e (diff) | |
| download | perlweeklychallenge-club-949b5020dc980a81bf593d19be3e6f3258d8a4c9.tar.gz perlweeklychallenge-club-949b5020dc980a81bf593d19be3e6f3258d8a4c9.tar.bz2 perlweeklychallenge-club-949b5020dc980a81bf593d19be3e6f3258d8a4c9.zip | |
Merge pull request #8109 from deoac/challenge-217
Challenge 217
| -rw-r--r-- | challenge-217/shimon-ben-avraham/raku/ch-1.md | 154 | ||||
| -rw-r--r-- | challenge-217/shimon-ben-avraham/raku/ch-2.md | 94 | ||||
| -rw-r--r-- | challenge-217/shimon-ben-avraham/raku/ch-2.raku | 51 | ||||
| -rwxr-xr-x | challenge-217/shimon-ben-avraham/raku/max-number.raku | 96 | ||||
| -rw-r--r-- | template.raku | 51 |
5 files changed, 292 insertions, 154 deletions
diff --git a/challenge-217/shimon-ben-avraham/raku/ch-1.md b/challenge-217/shimon-ben-avraham/raku/ch-1.md deleted file mode 100644 index 280ffd2275..0000000000 --- a/challenge-217/shimon-ben-avraham/raku/ch-1.md +++ /dev/null @@ -1,154 +0,0 @@ -<<<<<<< HEAD -TITLE -===== - -======= -```raku -# Perl Weekly Challenge #216, Challenge 1 -# © 2023 Shimon Bollinger. All rights reserved. -# Last modified: Thu 11 May 2023 07:47:20 PM EDT -# Version 0.0.1 -``` - -TITLE -===== - - - ->>>>>>> 0c0e20987dc5677805c95f8e2471492515be32c3 -Task 1: Registration Number -=========================== - -SUBTITLE -======== - - - -Submitted by: Mohammad S Anwar ------------------------------- - -You are given a list of words and a random registration number. - -Write a script to find all the words in the given list that has every letter in the given registration number. - -### Example 1 - - Input: @words = ('abc', 'abcd', 'bcd'), $reg = 'AB1 2CD' - Output: ('abcd') - -The only word that matches every alphabets in the given registration number is 'abcd'. - -### Example 2 - - Input: @words = ('job', 'james', 'bjorg'), $reg = '007 JB' - Output: ('job', 'bjorg') - -### Example 3 - - Input: @words = ('crack', 'road', 'rac'), $reg = 'C7 RA2' - Output: ('crack', 'rac') - -SOLUTION -======== - - - -```raku -multi MAIN (Str $reg, *@words) { - my Str @result; -``` - -We're going to use the `comb` method to determine if any of the input words fulfill the requirements. - -First, remove all the non-alphabetics from the registration number. - -```raku - my Junction $reg-letters = $reg.subst(/<-alpha>/, :g) -``` - -We don't care about the case of the letters. - -```raku - .lc -``` - -Now create an array of the letters remaing in the registration number. - -```raku - .comb -``` - -Create an `any` junction from the letters. - -```raku - .any; -``` - -In example 3, above, `$reg-letters` will be - -```raku -any("a", "r", "c") -``` - -Now, collect the words that match the registration in the `@result` array. - -```raku - @result.push($_) if -``` - -We don't care about the case of the words being tested. - -```raku - .lc -``` - -Here's the heart of the algorithm. `comb` the word with the previously made `Junction`. This will create a `Junction` of *n* `Seq`uences, where *n* is the number of letters in the registration number. - -```raku - .comb($reg-letters) -``` - -When we `comb` the words in the third example, we will get these `Junction`s: - -```raku -# for "crack" -any(("c", "c").Seq, ("r",).Seq, ("a",).Seq) - -# for "road" -any(().Seq, ("r",).Seq, ("a",).Seq) - -# for "rac" -any(("c",).Seq, ("r",).Seq, ("a",).Seq) -``` - -The word "road" does not meet the challenge's requirements, because it lacks the letter 'c'. This is reflected in the empty `Seq`uence: - -```raku -().Seq -``` - -The other two examples pass the requirements, and so do not include an empty `Seq`uence in their `any Junction`s. =end pod - -so to find the good words, match the `Junction` against an empty list; those that **don't** match are the good ones. - -```raku - !~~ () -``` - -Finally, make sure we test every word in the `@words` array... - -```raku - for @words; -``` - -...and print the results! - -```raku -<<<<<<< HEAD - say @result.List; -} # end of multi MAIN (Str $reg, *@words) - -======= - say @result.map({"'$_'"}).join(', ').map({ ($_) } ); -} # end of multi MAIN (Str $reg, *@words) ->>>>>>> 0c0e20987dc5677805c95f8e2471492515be32c3 diff --git a/challenge-217/shimon-ben-avraham/raku/ch-2.md b/challenge-217/shimon-ben-avraham/raku/ch-2.md new file mode 100644 index 0000000000..4e1bf3c359 --- /dev/null +++ b/challenge-217/shimon-ben-avraham/raku/ch-2.md @@ -0,0 +1,94 @@ +TITLE +===== + + + +Task 2: Max Number +------------------ + +SUBTITLE +======== + + + +Submitted by: Mohammad S Anwar +------------------------------ + +CHALLENGE +========= + + + +You are given a list of positive integers. Write a script to concatenate the integers to form the highest possible value. +------------------------------------------------------------------------------------------------------------------------- + +### Example 1: + + Input: @list = (1, 23) + + Output: 231 + +### Example 2: + + Input: @list = (10, 3, 2) + + Output: 3210 + +### Example 3: + + Input: @list = (31, 2, 4, 10) + + Output: 431210 + +### Example 4: + + Input: @list = (5, 15, 4, 123, 2) + + Output: 542111 + +### Example 5: + + Input: @list = (1, 10) + + Output: 110 + +SOLUTION +======== + + + +```raku + 1| use v6.*; + 2| + 3| sub max-number-cmp (UInt:D $a, UInt:D $b --> Order:D) { + 4| my @a=$a.comb; + 5| my @b=$b.comb; + 6| my $l = max(+@a, +@b); + 7| for ^$l -> $i { + 8| my $a = @a[$i] // @a[$i-1]; + 9| my $b = @b[$i] // @b[$i-1]; + 10| return $b <=> $a unless $a == $b; + 11| } + 12| return Same; + 13| } + 14| + 15| sub max-number (@list where .all ~~ UInt --> UInt) { + 16| return @list.sort(&max-number-cmp).join.UInt; + 17| } + 18| + 19| multi MAIN (:$test!) { + 20| use Test; + 21| + 22| my @tests = [ + 23| %{ input => (1, 23), output => 231, text => 'Example 1' }, + 24| %{ input => (10, 3, 2), output => 3210, text => 'Example 2' }, + 25| %{ input => (31, 2, 4, 10), output => 431210, text => 'Example 3' }, + 26| %{ input => (5, 11, 4, 1, 2), output => 542111, text => 'Example 4' }, + 27| %{ input => (1, 10), output => 110, text => 'Example 5' }, + 28| ]; + 29| + 30| for @tests { + 31| is max-number( .<input> ), .<output>, .<text>; + 32| } # end of for @tests + 33| } # end of multi MAIN (:$test! ) +``` diff --git a/challenge-217/shimon-ben-avraham/raku/ch-2.raku b/challenge-217/shimon-ben-avraham/raku/ch-2.raku new file mode 100644 index 0000000000..7beb6d55d3 --- /dev/null +++ b/challenge-217/shimon-ben-avraham/raku/ch-2.raku @@ -0,0 +1,51 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge 217-2 +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Sat 20 May 2023 11:16:24 PM EDT +# Version 0.0.1 + + +# always use the latest version of Raku +use v6.*; + +# We need a special comparison function. +# It will compare each digit in the two numbers. +# If one number has more digits than the other, +# the longer one always compares its remaining digits +# to the last digit of the shorter one. +sub max-number-cmp (UInt:D $a, UInt:D $b --> Order:D) { + my @a=$a.comb; + my @b=$b.comb; + my $l = max(+@a, +@b); + for ^$l -> $i { + my $a = @a[$i] // @a[$i-1]; + my $b = @b[$i] // @b[$i-1]; + return $b <=> $a unless $a == $b; + } + return Same; +} # end of my sub a (UInt:D $a, UInt:D $b --> Order:D) + +# Here's the solution to the challenge. It uses the comparison function above. +sub max-number (@list where .all ~~ UInt --> UInt) { + return @list.sort(&max-number-cmp).join.UInt; +} # end of sub max-number (@list where .all ~~ UInt --> UInt) + +# run `raku max-number.raku --test` to verify that all the examples work. +multi MAIN (:$test!) { + use Test; + + my @tests = [ + %{ input => (1, 23), output => 231, text => 'Example 1' }, + %{ input => (10, 3, 2), output => 3210, text => 'Example 2' }, + %{ input => (31, 2, 4, 10), output => 431210, text => 'Example 3' }, + %{ input => (5, 11, 4, 1, 2), output => 542111, text => 'Example 4' }, + %{ input => (1, 10), output => 110, text => 'Example 5' }, + ]; + + for @tests { + is max-number( .<input> ), .<output>, .<text>; + } # end of for @tests +} # end of multi MAIN (:$test! ) + + diff --git a/challenge-217/shimon-ben-avraham/raku/max-number.raku b/challenge-217/shimon-ben-avraham/raku/max-number.raku new file mode 100755 index 0000000000..8d1c3c3bce --- /dev/null +++ b/challenge-217/shimon-ben-avraham/raku/max-number.raku @@ -0,0 +1,96 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge 217-2 +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Sat 20 May 2023 11:16:24 PM EDT +# Version 0.0.1 + +=begin pod +=TITLE +=head2 Task 2: Max Number + +=SUBTITLE +=head2 Submitted by: Mohammad S Anwar + +=CHALLENGE +=head2 +You are given a list of positive integers. +Write a script to concatenate the integers to form the highest possible value. + +=head3 Example 1: + + Input: @list = (1, 23) + + Output: 231 + +=head3 Example 2: + + Input: @list = (10, 3, 2) + + Output: 3210 + +=head3 Example 3: + + Input: @list = (31, 2, 4, 10) + + Output: 431210 + +=head3 Example 4: + + Input: @list = (5, 15, 4, 123, 2) + + Output: 542111 + +=head3 Example 5: + + Input: @list = (1, 10) + + Output: 110 + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +# We need a special comparison function. +# It will compare each digit in the two numbers. +# If one number has more digits than the other, +# the longer one always compares its remaining digits +# to the last digit of the shorter one. +sub max-number-cmp (UInt:D $a, UInt:D $b --> Order:D) { + my @a=$a.comb; + my @b=$b.comb; + my $l = max(+@a, +@b); + for ^$l -> $i { + my $a = @a[$i] // @a[$i-1]; + my $b = @b[$i] // @b[$i-1]; + return $b <=> $a unless $a == $b; + } + return Same; +} # end of my sub a (UInt:D $a, UInt:D $b --> Order:D) + +# Here's the solution to the challenge. It uses the comparison function above. +sub max-number (@list where .all ~~ UInt --> UInt) { + return @list.sort(&max-number-cmp).join.UInt; +} # end of sub max-number (@list where .all ~~ UInt --> UInt) + +# run `raku max-number.raku --test` to verify that all the examples work. +multi MAIN (:$test!) { + use Test; + + my @tests = [ + %{ input => (1, 23), output => 231, text => 'Example 1' }, + %{ input => (10, 3, 2), output => 3210, text => 'Example 2' }, + %{ input => (31, 2, 4, 10), output => 431210, text => 'Example 3' }, + %{ input => (5, 11, 4, 1, 2), output => 542111, text => 'Example 4' }, + %{ input => (1, 10), output => 110, text => 'Example 5' }, + ]; + + for @tests { + is max-number( .<input> ), .<output>, .<text>; + } # end of for @tests +} # end of multi MAIN (:$test! ) + + diff --git a/template.raku b/template.raku new file mode 100644 index 0000000000..85d9896ff3 --- /dev/null +++ b/template.raku @@ -0,0 +1,51 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Mon 15 May 2023 09:17:32 PM EDT +# Version 0.0.1 + +=begin pod +=TITLE +=head2 Task + +=SUBTITLE +=head2 Submitted by + +=CHALLENGE +=head2 + +=head3 Example 1: + +=head3 Example 2: + +=head3 Example 3: + +=head3 Example 4: + +=head3 Example 5: + +=SOLUTION + +=end pod + +# always use the latest version of Raku +use v6.*; + +sub my-sub () { +; +} # end of sub my-sub () + +multi MAIN (:$test! ) { + use Test; + + my @tests = [ + %{ input => (), output => Nil, text => 'Example ' }, + ]; + + for @tests { + is my-sub( .<input> ), .<output>, .<text>; + } # end of for @tests +} # end of multi MAIN (:$test! ) + + |
