aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-05-08 00:25:06 +0100
committerGitHub <noreply@github.com>2023-05-08 00:25:06 +0100
commit635acf3f0ec8cb4730a2f47c2832bc2f7090de06 (patch)
tree076b4992aefe0ff692dd9d7e49a3ccfb6f5714dc
parent550d01773b8f447fe2d62be0bf7ff2bfe7a42330 (diff)
parent0153534a8a55b1f933dbbb0589efa203e0a4abe5 (diff)
downloadperlweeklychallenge-club-635acf3f0ec8cb4730a2f47c2832bc2f7090de06.tar.gz
perlweeklychallenge-club-635acf3f0ec8cb4730a2f47c2832bc2f7090de06.tar.bz2
perlweeklychallenge-club-635acf3f0ec8cb4730a2f47c2832bc2f7090de06.zip
Merge pull request #8040 from Util/c215
Add TWC 215 solutions by Bruce Gray (Raku only).
-rw-r--r--challenge-215/bruce-gray/raku/ch-1.raku41
-rw-r--r--challenge-215/bruce-gray/raku/ch-2.raku65
2 files changed, 106 insertions, 0 deletions
diff --git a/challenge-215/bruce-gray/raku/ch-1.raku b/challenge-215/bruce-gray/raku/ch-1.raku
new file mode 100644
index 0000000000..6bcf487f5c
--- /dev/null
+++ b/challenge-215/bruce-gray/raku/ch-1.raku
@@ -0,0 +1,41 @@
+sub task1 { +@^words.grep: { not [le] .comb } }
+
+# The single line above is my offical solution.
+
+# But wait! The task description technically
+# specifies an (un?)important difference:
+# Write a script to remove all words
+# not sorted alphabetically
+# and print the number of words in the list
+# that are not alphabetically sorted.
+# That `remove` is not reflected in the example output,
+# but I did it here wanyway, for maximum exactitude (or something).
+sub task1_more_correct ( @words --> UInt ) { # Also modifies the @words input array!!!
+
+ # Locate the positions of the unwanted elements.
+ my @indexes_to_delete = @words.grep: :k, { not [le] .comb };
+
+ # Remove the unwanted, modifying the input array.
+ # Must be done backwards, to prevent shifting index numbers.
+ @words.splice($_, 1) for @indexes_to_delete.reverse;
+
+ return +@indexes_to_delete;
+}
+
+
+my @tests =
+ ( <abc xyz tsu> , 1 ),
+ ( <rat cab dad> , 3 ),
+ ( <x y z> , 0 ),
+
+ ( <aaa bbb ccc> , 0 ),
+;
+use Test;
+plan 2 * @tests;
+for @tests -> (@in, $expected) {
+ is task1(@in), $expected, "task1 @in[]";
+}
+
+for @tests -> (@in is copy, $expected) { # `is copy` needed; it allows the sub to modify the array.
+ is task1_more_correct(@in), $expected, "task1_more_correct @in[]";
+}
diff --git a/challenge-215/bruce-gray/raku/ch-2.raku b/challenge-215/bruce-gray/raku/ch-2.raku
new file mode 100644
index 0000000000..ef7f74f8f4
--- /dev/null
+++ b/challenge-215/bruce-gray/raku/ch-2.raku
@@ -0,0 +1,65 @@
+# As long as we don't mind calculating *all* the places the zeros could go,
+# the task cleanly separates into "how many can we fit" and "is that >= wanted".
+sub task2 ( UInt $count, @ns --> Bool ) { $count <= max_zeros_space(@ns) }
+sub max_zeros_space ( @ns --> UInt ) {
+ return @ns.join
+ .split( / 0? 1+ 0? /, :skip-empty )
+ .map({ ( .chars / 2 ).ceiling })
+ .sum;
+}
+
+# This solution features inspectable modifications to $s,
+# and returns as soon as $count is reached.
+sub task2_with_early_return ( UInt $count, @ns --> Bool ) {
+ my $s = @ns.join;
+
+ for ^$count {
+ $s ~~ s/ <!after 1> 0 <!before 1> /1/
+ or return False;
+ }
+
+ return True;
+}
+
+# This solution avoids .join'ing the array, which would work better if
+# the 0|1 values ever needed to be longer than length=1 symbols.
+sub task2_without_join ( UInt $count is copy, @ns is copy --> Bool ) {
+ return True if $count == 0;
+
+ for @ns.keys -> $i {
+ if ( @ns[$i ] eq '0' )
+ and ( $i == 0 or @ns[$i-1] eq '0' )
+ and ( $i == @ns.end or @ns[$i+1] eq '0' ) {
+ @ns[$i] = '1';
+ $count--;
+ return True if $count == 0;
+ }
+ }
+
+ return False;
+}
+
+
+my @tests =
+ # From original task examples:
+ ( 1, (1,0,0,0,1) , 1 ),
+ ( 2, (1,0,0,0,1) , 0 ),
+ ( 3, (1,0,0,0,0,0,0,0,1) , 1 ),
+
+ # Checking edges:
+ ( 0, (1,0,0,1) , 1 ),
+ ( 1, (1,0,0,1) , 0 ),
+ ( 4, (1,0,0,0,0,0,0,0,1) , 0 ),
+
+ # Checking groupings:
+ ( 5, (1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1) , 1 ),
+ ( 6, (1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1) , 0 ),
+;
+use Test;
+plan 3 * @tests;
+
+for :&task2, :&task2_with_early_return, :&task2_without_join -> (:key($sub_name), :value($sub_to_test)) {
+ for @tests -> ($in_count, $in_ns, $expected) {
+ is +$sub_to_test($in_count, $in_ns), $expected, " $sub_name : {$in_ns.join}";
+ }
+}