aboutsummaryrefslogtreecommitdiff
path: root/challenge-197
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2023-01-01 23:18:45 +0000
committerGitHub <noreply@github.com>2023-01-01 23:18:45 +0000
commit091899afaafe37e8ac7ad0b27910683d1bf57736 (patch)
tree97b776a0c848a781c72f94b905310590c89a4ed1 /challenge-197
parent0807c50780be296a00bb3dc3e7653cb529fc362a (diff)
parent8010b9bce1cdbbbd3625fc9ddf5b66c65655f829 (diff)
downloadperlweeklychallenge-club-091899afaafe37e8ac7ad0b27910683d1bf57736.tar.gz
perlweeklychallenge-club-091899afaafe37e8ac7ad0b27910683d1bf57736.tar.bz2
perlweeklychallenge-club-091899afaafe37e8ac7ad0b27910683d1bf57736.zip
Merge pull request #7335 from Util/branch-for-challenge-197
Add TWC 197 solutions by Bruce Gray (Raku only).
Diffstat (limited to 'challenge-197')
-rw-r--r--challenge-197/bruce-gray/raku/ch-1.raku17
-rw-r--r--challenge-197/bruce-gray/raku/ch-2.raku87
2 files changed, 104 insertions, 0 deletions
diff --git a/challenge-197/bruce-gray/raku/ch-1.raku b/challenge-197/bruce-gray/raku/ch-1.raku
new file mode 100644
index 0000000000..bbf497839d
--- /dev/null
+++ b/challenge-197/bruce-gray/raku/ch-1.raku
@@ -0,0 +1,17 @@
+# sub task1 (@a) { @a.sort(* == 0) }
+
+sub move_to_end ( @a, &func ) { return @a.sort(&func) }
+
+sub task1 ( @ns ) { @ns.&move_to_end( * == 0 ) }
+
+
+my @tests =
+ ( (1, 0, 3, 0, 0, 5) , (1, 3, 5, 0, 0, 0) ),
+ ( (1, 6, 4) , (1, 6, 4) ),
+ ( (0, 1, 0, 2, 0) , (1, 2, 0, 0, 0) ),
+;
+use Test;
+plan +@tests;
+for @tests -> ($in, $expected) {
+ is-deeply task1($in), $expected;
+}
diff --git a/challenge-197/bruce-gray/raku/ch-2.raku b/challenge-197/bruce-gray/raku/ch-2.raku
new file mode 100644
index 0000000000..354fd83ab4
--- /dev/null
+++ b/challenge-197/bruce-gray/raku/ch-2.raku
@@ -0,0 +1,87 @@
+sub task2_splice_zip ( @ns ) {
+ my @a = @ns.sort(+*);
+ my @y = @a;
+ my @x = @y.splice(0, * div 2);
+ return flat ( @x.reverse Z @y.reverse );
+}
+sub task2_skip_head_zip ( @ns ) {
+ my @a = @ns.sort(-*);
+ my @x = @a.skip(+@a div 2);
+ my @y = @a.head(+@a div 2);
+ return flat ( @x Z @y );
+}
+multi sub task2_scsv ( @ns where * %% 2 ) {
+ return flat [Z] @ns.sort(-*).classify({ $++ < (+@ns div 2) }).sortĀ».value;
+ # Or:
+ # return @ns.sort(-*).sort({ ($++).polymod(+@ns div 2) Z* (1,-1) });
+}
+multi sub task2_scsv ( @ns where * !%% 2 ) {
+ my @r = @ns.sort(-*);
+
+ my $z = @r.pop;
+ @r = samewith(@r);
+ @r.push($z);
+
+ return @r.List;
+}
+sub task2_feed_by_key ( @ns ) {
+ my @s = @ns.sort(-*);
+
+ my @r;
+ @r[ .key * 2 + 1 ] = .value for @s.head(+@s div 2).pairs;
+ @r[ .key * 2 + 0 ] = .value for @s.skip(+@s div 2).pairs;
+
+ return @r.List;
+}
+
+
+my @tests =
+ ( (1,5,1,1,6,4) , (1,6,1,5,1,4) ),
+ ( (1,3,2,2,3,1) , (2,3,1,3,1,2) ),
+ ( (3,4,5,6,7 ) , (5,7,4,6,3) ),
+;
+sub is-wiggly ( @ns --> Bool ) {
+ return ?( @ns.skip(0).rotor(2).map({ .[0] < .[1] }).all
+ and @ns.skip(1).rotor(2).map({ .[0] > .[1] }).all );
+}
+# Pre-check that all the expected results are valid.
+for @tests -> ($in, $expected) {
+ # .[0] < .[1] > .[2] or die for $expected.list.rotor(3 => -1); #XXX world not check if < 3 elements
+ .[0] < .[1] or die for $expected.skip(0).rotor(2);
+ .[0] > .[1] or die for $expected.skip(1).rotor(2);
+ .&is-wiggly or die for $expected;
+ $in.elems == $expected.elems or die;
+}
+
+my @subs =
+ :&task2_splice_zip,
+ :&task2_skip_head_zip,
+ :&task2_feed_by_key,
+ :&task2_scsv
+;
+my Set $sub_names_that_can_handle_even_and_odd = set <
+ task2_scsv
+ task2_feed_by_key
+>;
+
+die "Duplicate seen!" if @subsĀ».key.repeated;
+if $sub_names_that_can_handle_even_and_odd (-) @subs>>.key.Set -> Set $bad {
+ die "Sub name not in @subs : $bad";
+}
+
+use Test;
+plan +@tests * +@subs;
+
+for @tests -> ($in, $expected) {
+ my $test_num = ++$;
+
+ for @subs {
+ my ( $sub_name, $sub_code ) = .kv;
+ if $in.elems !%% 2 and !($sub_name (elem) $sub_names_that_can_handle_even_and_odd) {
+ skip "Cannot handle odd sizes : $test_num : $sub_name ";
+ next;
+ }
+
+ is-deeply $sub_code.($in), $expected, "$test_num : $sub_name";
+ }
+}