aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrir <rirans@comcast.net>2025-06-05 22:35:15 -0400
committerrir <rirans@comcast.net>2025-06-05 22:35:15 -0400
commit8ea6085ce74fd0c0e33253d557d2a194fb29ed75 (patch)
treea75c91698e1eaaf2e4b4b9f027cedb0d3119f474
parentb0159e77cc4e56da3a5a1c86d5769c652bcc887e (diff)
downloadperlweeklychallenge-club-8ea6085ce74fd0c0e33253d557d2a194fb29ed75.tar.gz
perlweeklychallenge-club-8ea6085ce74fd0c0e33253d557d2a194fb29ed75.tar.bz2
perlweeklychallenge-club-8ea6085ce74fd0c0e33253d557d2a194fb29ed75.zip
324
-rw-r--r--challenge-324/0rir/raku/ch-1.raku61
-rw-r--r--challenge-324/0rir/raku/ch-2.raku63
2 files changed, 124 insertions, 0 deletions
diff --git a/challenge-324/0rir/raku/ch-1.raku b/challenge-324/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..16c0fb1d08
--- /dev/null
+++ b/challenge-324/0rir/raku/ch-1.raku
@@ -0,0 +1,61 @@
+#!/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
+324-1: 2D Array Submitted by: Mohammad Sajid Anwar
+You are given an array of integers and two integers $r amd $c.
+
+Write a script to create two dimension array having $r rows and $c columns using the given array.
+
+
+Example 1
+Input: @ints = (1, 2, 3, 4), $r = 2, $c = 2
+Output: ([1, 2], [3, 4])
+
+Example 2
+Input: @ints = (1, 2, 3), $r = 1, $c = 3
+Output: ([1, 2, 3])
+
+Example 3
+Input: @ints = (1, 2, 3, 4), $r = 4, $c = 1
+Output: ([1], [2], [3], [4])
+
+=end comment
+
+my @Test =
+ { ary => (1, 2, 3, 4), rows => 2, cols => 2, exp => ([1, 2], [3, 4]) },
+ { ary => (1, 2, 3), rows => 1, cols => 3, exp => ([1, 2, 3],) },
+ { ary => (1, 2, 3, 4), rows => 4, cols => 1, exp => ([1], [2], [3], [4]) },
+;
+my @Die =
+ { ary => (1, 2, 3), rows => 2, cols => 3, },
+ { ary => (1, 2, 3), rows => -1, cols => -3, },
+ { ary => (), rows => 2, cols => 3, },
+;
+plan +@Test + @Die;
+
+sub task( List:D $ary, Int:D $rows, Int:D $cols -->Array) {
+ die 'Shape not possible' if $ary.elems ≠ $rows × $cols or $rows < 0;
+ return (@$ary.rotor( $cols)».Array).Array;
+}
+
+for @Test -> %h {
+ is task( %h<ary>, %h<rows>, %h<cols>), %h<exp>,
+ "%h<exp>.raku() <- [%h<ary>]∘∘ %h<rows> X %h<cols>";
+}
+for @Die -> %h {
+ dies-ok { task( %h<ary>, %h<rows>, %h<cols>)}, 'Illegal inputs'
+}
+done-testing;
+
+my @int = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
+my $rows = 3;
+my $cols = 4;
+say qq{\nInput: @int = @int.raku(), \$rows = $rows, \$cols = $cols\n}
+ ~ "Output: {task(@int, $rows, $cols).raku} ";
+
+
diff --git a/challenge-324/0rir/raku/ch-2.raku b/challenge-324/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..918b5d228d
--- /dev/null
+++ b/challenge-324/0rir/raku/ch-2.raku
@@ -0,0 +1,63 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉⊆ ≡ ≢ «␤ » ∴
+
+use v6.e.PREVIEW; # for .are
+use Test;
+
+=begin comment
+324-2: Total XOR Submitted by: Mohammad Sajid Anwar
+You are given an array of integers.
+
+Write a script to return the sum of total XOR for every subset of given array.
+
+Example 1
+Input: @ints = (1, 3)
+Output: 6
+
+Subset [1], total XOR = 1
+Subset [3], total XOR = 3
+Subset [1, 3], total XOR => 1 XOR 3 => 2
+
+Sum of total XOR => 1 + 3 + 2 => 6
+
+Example 2
+Input: @ints = (5, 1, 6)
+Output: 28
+
+Subset [5], total XOR = 5
+Subset [1], total XOR = 1
+Subset [6], total XOR = 6
+Subset [5, 1], total XOR => 5 XOR 1 => 4
+Subset [5, 6], total XOR => 5 XOR 6 => 3
+Subset [1, 6], total XOR => 1 XOR 6 => 7
+Subset [5, 1, 6], total XOR => 5 XOR 1 XOR 6 => 2
+
+Sum of total XOR => 5 + 1 + 6 + 4 + 3 + 7 + 2 => 28
+
+Example 3
+Input: @ints = (3, 4, 5, 6, 7, 8)
+Output: 480
+=end comment
+
+my @Test =
+ # @in $exp
+ (1, 3), 6,
+ (5, 1, 6), 28,
+ (3, 4, 5, 6, 7, 8), 480,
+ (-1, -3) , -2,
+ (), 0,
+;
+
+plan +@Test ÷ 2;
+
+sub task( @a where { **.are(Int) or ** ~~ Empty } -->Int:D) {
+ sum do for @a.combinations { sum [+^] $_ }
+}
+
+for @Test -> @in, $exp {
+ is task( @in), $exp, "{$exp // $exp.^name()} <- @in.raku()";
+}
+done-testing;
+my @int = 7, 11, 31;
+
+say "\nInput: @int = @int.raku()\nOutput: {task @int}";