aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrir <rirans@comcast.net>2025-01-23 20:30:24 -0500
committerrir <rirans@comcast.net>2025-01-23 20:34:06 -0500
commit7cd6c3be1fb834a6a950911c965236eda1ed93b3 (patch)
treef17e5d400e0a4ed7d41de7fe9d0d332e0501562a
parent5e6595eac1c00b30cad16ac213fa97bf72e58459 (diff)
downloadperlweeklychallenge-club-7cd6c3be1fb834a6a950911c965236eda1ed93b3.tar.gz
perlweeklychallenge-club-7cd6c3be1fb834a6a950911c965236eda1ed93b3.tar.bz2
perlweeklychallenge-club-7cd6c3be1fb834a6a950911c965236eda1ed93b3.zip
305
-rw-r--r--challenge-305/0rir/raku/ch-1.raku79
-rw-r--r--challenge-305/0rir/raku/ch-2.raku116
2 files changed, 195 insertions, 0 deletions
diff --git a/challenge-305/0rir/raku/ch-1.raku b/challenge-305/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..eb8a16cbc2
--- /dev/null
+++ b/challenge-305/0rir/raku/ch-1.raku
@@ -0,0 +1,79 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴
+use v6.d;
+INIT $*RAT-OVERFLOW = FatRat;
+use lib $?FILE.IO.cleanup.parent(2).add("lib");
+use Test;
+
+=begin comment
+305-1: Binary Prefix Submitted by: Mohammad Sajid Anwar
+You are given a binary array.
+
+Write a script to return an array of booleans where the partial binary number up to that point is prime.
+
+Example 1
+Input: @binary = (1, 0, 1)
+Output: (false, true, true)
+
+Sub-arrays (base-10):
+(1): 1 - not prime
+(1, 0): 2 - prime
+(1, 0, 1): 5 - prime
+Example 2
+Input: @binary = (1, 1, 0)
+Output: (false, true, false)
+
+Sub-arrays (base-10):
+(1): 1 - not prime
+(1, 1): 3 - prime
+(1, 1, 0): 6 - not prime
+Example 3
+Input: @binary = (1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1)
+Output: (false, true, true, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, true)
+
+=end comment
+
+constant \f = False;
+constant \t = True;
+
+my @Test =
+ # in out
+ (1, 0, 1), (f, t, t),
+ (1, 1, 0), (f, t, f),
+ (1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1),
+ (f, t, t, f, f, t, f, f, f, f, f, f, f, f, f, f, f, f, f, t),
+;
+plan 2 × @Test ÷ 2;
+
+sub task1( @a) {
+ do for ^@a {
+ @a[0..$_].join.parse-base(2).is-prime;
+ }
+}
+sub task2( @a) {
+ my $str = @a.join;
+ return do for 1..$str.chars {
+ $str.substr(0, $_).parse-base(2).is-prime;
+ }
+}
+
+for @Test -> @in, @out {
+ is task1( @in), @out, (@out».Str).join(' ').raku() ~ " <- @in.raku()";
+ is task2( @in), @out, (@out».Str).join(' ').raku() ~ " <- @in.raku()";
+}
+
+
+done-testing;
+
+use Bench;
+my $b = Bench.new;
+
+$b.cmpthese(10_000, {
+ Ary-y => sub { for @Test -> @in, @junk { task1( @in) } },
+ Str-y => sub { for @Test -> @in, @junk { task2( @in) } },
+});
+
+
+my @binary = 1, 0, 1, 1, 0, 1, 1, 1;
+say "\nInput: @binary = @binary[]\nOutput: { task2 @binary }";
+
diff --git a/challenge-305/0rir/raku/ch-2.raku b/challenge-305/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..7706799032
--- /dev/null
+++ b/challenge-305/0rir/raku/ch-2.raku
@@ -0,0 +1,116 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴
+use v6.d;
+use Test;
+
+=begin comment
+305-2: Alien Dictionary Submitted by: Mohammad Sajid Anwar
+You are given a list of words and alien dictionary character order.
+
+Write a script to sort lexicographically the given list of words based
+on the alien dictionary characters.
+
+Example 1
+Input: @words = ("perl", "python", "raku")
+ @alien = qw/h l a b y d e f g i r k m n o p q j s t u v w x c z/
+Output: ("raku", "python", "perl")
+Example 2
+Input: @words = ("the", "weekly", "challenge")
+ @alien = qw/c o r l d a b t e f g h i j k m n p q s w u v x y z/
+Output: ("challenge", "the", "weekly")
+=end comment
+
+=begin comment
+ What is a word is vague.
+=end comment
+
+my %alien =
+ rev =>[qw{a b c d e f g h i j k l m n o p q r s t u v w x y z}.reverse],
+ hla => qw/h l a b y d e f g i r k m n o p q j s t u v w x c z/,
+ cor => qw/c o r l d a b t e f g h i j k m n p q s w u v x y z/,
+;
+
+my @Test =
+ { words => (), alien => 'cor',
+ exp => (),
+ exp-i => (),
+ },
+ { words => ("perl", "python", "raku"), alien => 'hla',
+ exp => ("raku", "python", "perl"),
+ exp-i => ("raku", "python", "perl"),
+ },
+ { words => ("the", "weekly", "challenge"), alien => 'cor',
+ exp => ("challenge", "the", "weekly"),
+ exp-i => ("challenge", "the", "weekly"),
+ },
+ { words => ("the-", "wee-kly", "cha-lle/n+ge"), alien => 'cor',
+ exp => ("cha-lle/n+ge", "the-", "wee-kly"),
+ exp-i => ("cha-lle/n+ge", "the-", "wee-kly"),
+ },
+ { words => ("the", "The"), alien => 'cor',
+ exp => ("The", "the"),
+ exp-i => ("the", "The"), # stability
+ },
+ { words => ("The", "the"), alien => 'cor',
+ exp => ("The", "the"),
+ exp-i => ("The", "the"),
+ },
+ { words => ("city", "Rocks"), alien => 'cor',
+ exp => ("Rocks", "city"),
+ exp-i => ("city", "Rocks"),
+ },
+ { words => ("tee", "tHe"), alien => 'cor',
+ exp => ("tHe", "tee"),
+ exp-i => ("tee", "tHe"),
+ },
+ { words => ("teEa", "tee"), alien => 'cor',
+ exp => ("teEa", "tee"),
+ exp-i => ("tee", "teEa"),
+ },
+;
+plan @Test × 2;
+
+constant \native = (' '… '~').List;
+
+# Make a Map for sorting using alien alphabets; a not-ascii map
+# ignoring control chars.
+only mk-map( List $alien --> Map ) {
+ ( ( (' '…'@'), @$alien».uc, ( '['… '`'), @$alien, ( '{' … '~') ).flat
+ [Z=>]
+ ( ' ' … '~')).Map;
+}
+
+# Build, and register, known alien alphabet maps.
+my %Reg = %alien;
+for %Reg.keys -> \k { %Reg{k} = %Reg{k}.&mk-map; }
+
+my %Map; # The current mapping in use.
+ # XXX! Eliminate by making intermediate func's which
+ # wrap task()'s return statements: @word.&alien-sort-i( %map);
+
+# Sort a list of words with a re-ordered alphabet. :i for ignore case.
+sub task( @word, $name, Bool :$i) {
+ my @w = @word;
+ %Map = %Reg{$name} // die qq{No alphabet named "$name"};
+ return @word.sort: &alien-cmp-i if $i;
+ return @word.sort: &alien-cmp;
+}
+
+sub alien-cmp( $a, $b -->Order) {
+ %Map{$a.comb}.join cmp %Map{$b.comb}.join;
+}
+
+sub alien-cmp-i( $a, $b -->Order) {
+ %Map{$a.comb».fc}.join cmp %Map{$b.comb».fc}.join;
+}
+
+for @Test -> %t {
+ is task( %t<words>, %t<alien> ), %t<exp>, "%t<exp> <-= %t<words>";
+ is task( %t<words>, %t<alien>, :i), %t<exp-i>, "%t<exp-i> <i= %t<words>";
+}
+done-testing;
+
+my @word ="The", "weEkly", "chalLenge", "No.150";
+my @alien = %alien<rev>;
+say "\nInput: @word = (@word[])\n\@alien: (@alien[])"
+ ~ "\nOutput: ", task(@word, 'rev', );