aboutsummaryrefslogtreecommitdiff
path: root/challenge-258/barroff/raku
diff options
context:
space:
mode:
authorBarrOff <58253563+BarrOff@users.noreply.github.com>2024-03-03 22:31:37 +0100
committerBarrOff <58253563+BarrOff@users.noreply.github.com>2024-03-03 22:31:37 +0100
commitfd63b53de29f11fbfc54aa7bd6d050db68f74bf7 (patch)
treeef0fd925d48dd2d9ff89b98babeeab89b4c17e43 /challenge-258/barroff/raku
parent0c4e2859ea107353498b5d0c1809151f600c9c2e (diff)
downloadperlweeklychallenge-club-fd63b53de29f11fbfc54aa7bd6d050db68f74bf7.tar.gz
perlweeklychallenge-club-fd63b53de29f11fbfc54aa7bd6d050db68f74bf7.tar.bz2
perlweeklychallenge-club-fd63b53de29f11fbfc54aa7bd6d050db68f74bf7.zip
feat: add solutions and blog for challenge 258 from BarrOff
Diffstat (limited to 'challenge-258/barroff/raku')
-rw-r--r--challenge-258/barroff/raku/ch-1.p622
-rw-r--r--challenge-258/barroff/raku/ch-2.p628
2 files changed, 50 insertions, 0 deletions
diff --git a/challenge-258/barroff/raku/ch-1.p6 b/challenge-258/barroff/raku/ch-1.p6
new file mode 100644
index 0000000000..fc62a4c73f
--- /dev/null
+++ b/challenge-258/barroff/raku/ch-1.p6
@@ -0,0 +1,22 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub count-even-digits-number(@ints --> Int:D) {
+ sum(map({ not( Str($_).chars % 2 ).Int }, @ints));
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 3;
+
+ is count-even-digits-number(( 10, 1, 111, 24, 1000 )), 3, 'works for ( 10, 1, 111, 24, 1000 )';
+ is count-even-digits-number(( 111, 1, 11111 )), 0, 'works for ( 111, 1, 11111 )';
+ is count-even-digits-number(( 2, 8, 1024, 256 )), 1, 'works for ( 2, 8, 1024, 256 )';
+}
+
+#| Take user provided number like 10 1 111 24 1000
+multi sub MAIN(Int:D @ints) {
+ say count-even-digits-number(@ints);
+}
diff --git a/challenge-258/barroff/raku/ch-2.p6 b/challenge-258/barroff/raku/ch-2.p6
new file mode 100644
index 0000000000..ad41fc218f
--- /dev/null
+++ b/challenge-258/barroff/raku/ch-2.p6
@@ -0,0 +1,28 @@
+#!/usr/bin/env raku
+
+use v6.d;
+
+sub sum-of-values(@ints, Int:D $k --> Int:D) {
+ return sum(
+ @ints[
+ grep(
+ { sum($_.base(2).comb) == $k }, 0..@ints.elems - 1
+ )
+ ]
+ )
+}
+
+#| Run test cases
+multi sub MAIN('test') {
+ use Test;
+ plan 3;
+
+ is sum-of-values((2, 5, 9, 11, 3), 1), 17, 'works for k = 1';
+ is sum-of-values((2, 5, 9, 11, 3), 2), 11, 'works for k = 2';
+ is sum-of-values((2, 5, 9, 11, 3), 0), 2, 'works for k = 0';
+}
+
+#| Take user provided number like 10 1 111 24 1000
+multi sub MAIN(Int:D $k, *@ints) {
+ say sum-of-values(@ints, $k);
+}