From 7457c38c951afa4c4b9bb43751c454d23baacbc2 Mon Sep 17 00:00:00 2001 From: deoac Date: Thu, 11 May 2023 17:57:57 -0400 Subject: Got it working --- challenge-216/shimon-ben-avraham/raku/ch-1.md | 157 +++++++++++++++++++++ challenge-216/shimon-ben-avraham/raku/ch-1.raku | 34 +++++ .../raku/registration-number.raku | 138 ++++++++++++++++++ 3 files changed, 329 insertions(+) create mode 100644 challenge-216/shimon-ben-avraham/raku/ch-1.md create mode 100755 challenge-216/shimon-ben-avraham/raku/ch-1.raku create mode 100755 challenge-216/shimon-ben-avraham/raku/registration-number.raku diff --git a/challenge-216/shimon-ben-avraham/raku/ch-1.md b/challenge-216/shimon-ben-avraham/raku/ch-1.md new file mode 100644 index 0000000000..81eaa28f55 --- /dev/null +++ b/challenge-216/shimon-ben-avraham/raku/ch-1.md @@ -0,0 +1,157 @@ +```raku +#! /usr/bin/env raku + +# Perl Weekly Challenge #216, Challenge 1 +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Thu 11 May 2023 05:53:06 PM EDT +# Version 0.0.1 + +# always use the latest version of Raku +use v6.*; +``` + +TITLE +===== + + + +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 + say @result.List; +} # end of multi MAIN (Str $reg, *@words) + +#| Run with the option '--test' to test the program +multi MAIN (Bool :$test!) { + use Test::Output; + + output-is {samewith('AB1 2CD', )}, "(abcd)\n", 'Example 1 OK'; + output-is {samewith('007 JB', )}, "(job bjorg)\n", 'Example 2 OK'; + output-is {samewith('C7 RA2', )}, "(crack rac)\n", 'Example 3 OK'; +} +``` diff --git a/challenge-216/shimon-ben-avraham/raku/ch-1.raku b/challenge-216/shimon-ben-avraham/raku/ch-1.raku new file mode 100755 index 0000000000..1b79eb963e --- /dev/null +++ b/challenge-216/shimon-ben-avraham/raku/ch-1.raku @@ -0,0 +1,34 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge #216, Challenge 1 +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Thu 11 May 2023 05:55:50 PM EDT +# Version 0.0.1 + +# always use the latest version of Raku +use v6.*; + + +multi MAIN (Str $reg, *@words) { + my Str @result; + my Junction $reg-letters = $reg.subst(/<-alpha>/, :g) + .lc + .comb + .any; + @result.push($_) if + .lc + .comb($reg-letters) + !~~ () for @words; + + say @result.List; +} # end of multi MAIN (Str $reg, *@words) + +#| Run with the option '--test' to test the program +multi MAIN (Bool :$test!) { + use Test::Output; + + output-is {samewith('AB1 2CD', )}, "(abcd)\n", 'Example 1 OK'; + output-is {samewith('007 JB', )}, "(job bjorg)\n", 'Example 2 OK'; + output-is {samewith('C7 RA2', )}, "(crack rac)\n", 'Example 3 OK'; +} + diff --git a/challenge-216/shimon-ben-avraham/raku/registration-number.raku b/challenge-216/shimon-ben-avraham/raku/registration-number.raku new file mode 100755 index 0000000000..2ad2105af5 --- /dev/null +++ b/challenge-216/shimon-ben-avraham/raku/registration-number.raku @@ -0,0 +1,138 @@ +#! /usr/bin/env raku + +# Perl Weekly Challenge #216, Challenge 1 +# © 2023 Shimon Bollinger. All rights reserved. +# Last modified: Thu 11 May 2023 05:55:17 PM EDT +# Version 0.0.1 + +# always use the latest version of Raku +use v6.*; + +=begin pod +=TITLE +=head1 Task 1: Registration Number + +=SUBTITLE + +=head2 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. + +=head3 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'. + +=head3 Example 2 + + Input: @words = ('job', 'james', 'bjorg'), $reg = '007 JB' + Output: ('job', 'bjorg') + +=head3 Example 3 + + Input: @words = ('crack', 'road', 'rac'), $reg = 'C7 RA2' + Output: ('crack', 'rac') + +=SOLUTION +=end pod + +multi MAIN (Str $reg, *@words) { + my Str @result; +=begin pod +We're going to use the C method to determine if any of the input words +fulfill the requirements. + +First, remove all the non-alphabetics from the registration number. +=end pod + my Junction $reg-letters = $reg.subst(/<-alpha>/, :g) +=begin pod +We don't care about the case of the letters. +=end pod + .lc +=begin pod + +Now create an array of the letters remaing in the registration number. + +=end pod + .comb +=begin pod +Create an C junction from the letters. + +=end pod + .any; +=begin pod + +In example 3, above, C<$reg-letters> will be +=begin code :lang +any("a", "r", "c") +=end code +Now, collect the words that match the registration in the C<@result> array. +=end pod + @result.push($_) if +=begin pod +We don't care about the case of the words being tested. +=end pod + .lc +=begin pod +Here's the heart of the algorithm. C the word with the previously +made C. This will create a C of I Cuences, where +I is the number of letters in the registration number. +=end pod + .comb($reg-letters) +=begin pod + +When we C the words in the third example, we will get these Cs: +=begin code :lang +# 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) + +=end code + +The word "road" does not meet the challenge's requirements, because it lacks +the letter 'c'. This is reflected in the empty Cuence: + +=begin code :lang +().Seq +=end code + +The other two examples pass the requirements, and so do not include an empty +Cuence in their Cs. =end pod + +so to find the good words, match the C against an empty list; those +that B match are the good ones. + +=end pod + !~~ () +=begin pod + +Finally, make sure we test every word in the C<@words> array... +=end pod + for @words; +=begin pod +...and print the results! +=end pod + + say @result.List; +} # end of multi MAIN (Str $reg, *@words) + +#| Run with the option '--test' to test the program +multi MAIN (Bool :$test!) { + use Test::Output; + + output-is {samewith('AB1 2CD', )}, "(abcd)\n", 'Example 1 OK'; + output-is {samewith('007 JB', )}, "(job bjorg)\n", 'Example 2 OK'; + output-is {samewith('C7 RA2', )}, "(crack rac)\n", 'Example 3 OK'; +} + -- cgit From b14bc029e36daaca7faa29f9ca38c653c9670c21 Mon Sep 17 00:00:00 2001 From: deoac Date: Thu, 11 May 2023 18:26:29 -0400 Subject: Got it working! --- challenge-216/shimon-ben-avraham/raku/ch-1.md | 23 ---------------------- challenge-216/shimon-ben-avraham/raku/ch-1.raku | 2 +- .../raku/registration-number.raku | 2 +- 3 files changed, 2 insertions(+), 25 deletions(-) diff --git a/challenge-216/shimon-ben-avraham/raku/ch-1.md b/challenge-216/shimon-ben-avraham/raku/ch-1.md index 81eaa28f55..5130597373 100644 --- a/challenge-216/shimon-ben-avraham/raku/ch-1.md +++ b/challenge-216/shimon-ben-avraham/raku/ch-1.md @@ -1,20 +1,6 @@ -```raku -#! /usr/bin/env raku - -# Perl Weekly Challenge #216, Challenge 1 -# © 2023 Shimon Bollinger. All rights reserved. -# Last modified: Thu 11 May 2023 05:53:06 PM EDT -# Version 0.0.1 - -# always use the latest version of Raku -use v6.*; -``` - TITLE ===== - - Task 1: Registration Number =========================== @@ -146,12 +132,3 @@ Finally, make sure we test every word in the `@words` array... say @result.List; } # end of multi MAIN (Str $reg, *@words) -#| Run with the option '--test' to test the program -multi MAIN (Bool :$test!) { - use Test::Output; - - output-is {samewith('AB1 2CD', )}, "(abcd)\n", 'Example 1 OK'; - output-is {samewith('007 JB', )}, "(job bjorg)\n", 'Example 2 OK'; - output-is {samewith('C7 RA2', )}, "(crack rac)\n", 'Example 3 OK'; -} -``` diff --git a/challenge-216/shimon-ben-avraham/raku/ch-1.raku b/challenge-216/shimon-ben-avraham/raku/ch-1.raku index 1b79eb963e..400c22ce2e 100755 --- a/challenge-216/shimon-ben-avraham/raku/ch-1.raku +++ b/challenge-216/shimon-ben-avraham/raku/ch-1.raku @@ -2,7 +2,7 @@ # Perl Weekly Challenge #216, Challenge 1 # © 2023 Shimon Bollinger. All rights reserved. -# Last modified: Thu 11 May 2023 05:55:50 PM EDT +# Last modified: Thu 11 May 2023 06:24:39 PM EDT # Version 0.0.1 # always use the latest version of Raku diff --git a/challenge-216/shimon-ben-avraham/raku/registration-number.raku b/challenge-216/shimon-ben-avraham/raku/registration-number.raku index 2ad2105af5..f8e06ded41 100755 --- a/challenge-216/shimon-ben-avraham/raku/registration-number.raku +++ b/challenge-216/shimon-ben-avraham/raku/registration-number.raku @@ -2,7 +2,7 @@ # Perl Weekly Challenge #216, Challenge 1 # © 2023 Shimon Bollinger. All rights reserved. -# Last modified: Thu 11 May 2023 05:55:17 PM EDT +# Last modified: Thu 11 May 2023 06:24:53 PM EDT # Version 0.0.1 # always use the latest version of Raku -- cgit