aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBarrOff <58253563+BarrOff@users.noreply.github.com>2025-06-08 22:02:28 +0200
committerBarrOff <58253563+BarrOff@users.noreply.github.com>2025-06-08 22:02:28 +0200
commit584721782052da79297e7846337d7d3733ba458f (patch)
tree6a8759158d97a385d88f890958a4b956237541c9
parent0d913a6375c00fa781af58cb88a3099afa833b99 (diff)
downloadperlweeklychallenge-club-584721782052da79297e7846337d7d3733ba458f.tar.gz
perlweeklychallenge-club-584721782052da79297e7846337d7d3733ba458f.tar.bz2
perlweeklychallenge-club-584721782052da79297e7846337d7d3733ba458f.zip
feat: add solutions for challenges 323 and 324 from BarrOff
-rw-r--r--challenge-323/barroff/raku/ch-1.p631
-rw-r--r--challenge-324/barroff/raku/ch-1.p624
-rw-r--r--challenge-324/barroff/raku/ch-2.p627
3 files changed, 82 insertions, 0 deletions
diff --git a/challenge-323/barroff/raku/ch-1.p6 b/challenge-323/barroff/raku/ch-1.p6
new file mode 100644
index 0000000000..97d8fdf02a
--- /dev/null
+++ b/challenge-323/barroff/raku/ch-1.p6
@@ -0,0 +1,31 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+my regex inc-dec { [ "++" | "--" ] }
+my regex operation { [ <inc-dec> "x" | "x" <inc-dec> ] };
+
+sub increment-decrement(@operations --> Int) {
+ my $count-positive = grep({ /\+\+/ }, @operations).elems;
+ 2 × $count-positive - @operations.elems;
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 3;
+
+ is increment-decrement(["--x", "x++", "x++"]), 1, 'works for "--x", "x++", "x++"';
+ is increment-decrement(["x++", "++x", "x++"]), 3, 'works for "x++", "++x", "x++"';
+ is increment-decrement(["x++", "++x", "--x", "x--"]), 0,
+ 'works for "x++", "++x", "--x", "x--"';
+}
+
+#| Take user provided strings like "x++" "++x" "--x" "x--"
+# multi sub MAIN(*@operations where @operations.all ~~ / <operation> /) {
+multi sub MAIN(
+ Bool :$x = False,
+ *@operations where @operations.all ~~ / <operation> /
+) {
+ say increment-decrement(@operations) - $x.Int ;
+}
diff --git a/challenge-324/barroff/raku/ch-1.p6 b/challenge-324/barroff/raku/ch-1.p6
new file mode 100644
index 0000000000..361f86f78b
--- /dev/null
+++ b/challenge-324/barroff/raku/ch-1.p6
@@ -0,0 +1,24 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub d2-array(Int $r, Int $c, @ints --> Seq) {
+ map({ @ints[$_ × $c ..^ ($_ + 1) × $c] }, 0..^$r);
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 3;
+
+ is d2-array(2, 2, [1, 2, 3, 4]), [[1, 2], [3, 4]],
+ 'works for "2, 2, [1, 2, 3, 4]"';
+ is d2-array(1, 3, [1, 2, 3]), [1, 2, 3], 'works for "1, 3, [1, 2, 3]"';
+ is d2-array(4, 1, [1, 2, 3, 4]), [[1], [2], [3], [4]],
+ 'works for "4, 1, [1, 2, 3, 4]"';
+}
+
+#| Take user provided number like "Perl Weekly Challenge" l a
+multi sub MAIN(Int $r, Int $c, *@ints) {
+ say d2-array($r, $c, @ints);
+}
diff --git a/challenge-324/barroff/raku/ch-2.p6 b/challenge-324/barroff/raku/ch-2.p6
new file mode 100644
index 0000000000..1217d28c65
--- /dev/null
+++ b/challenge-324/barroff/raku/ch-2.p6
@@ -0,0 +1,27 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub total-xor(@ints --> Int) {
+ sum(
+ map(
+ { reduce(&infix:<+^>, $_) },
+ combinations(@ints)
+ )
+ )
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 3;
+
+ is total-xor([1, 3]), 6, 'works for "[1, 3]"';
+ is total-xor([5, 1, 6]), 28, 'works for "[5, 1, 6]"';
+ is total-xor([3, 4, 5, 6, 7, 8]), 480, 'works for "[3, 4, 5, 6, 7, 8]"';
+}
+
+#| Take user provided numbers like 3, 4, 5, 6, 7, 8
+multi sub MAIN(@ints) {
+ say total-xor(@ints);
+}