aboutsummaryrefslogtreecommitdiff
path: root/challenge-258
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-258')
-rw-r--r--challenge-258/0rir/raku/ch-1.raku50
-rw-r--r--challenge-258/0rir/raku/ch-2.raku60
2 files changed, 110 insertions, 0 deletions
diff --git a/challenge-258/0rir/raku/ch-1.raku b/challenge-258/0rir/raku/ch-1.raku
new file mode 100644
index 0000000000..7d7f0e412c
--- /dev/null
+++ b/challenge-258/0rir/raku/ch-1.raku
@@ -0,0 +1,50 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+use Test;
+
+=begin comment
+258-1: Count Even Digits Number Submitted by: Mohammad Sajid Anwar
+You are given a array of positive integers, @ints.
+
+Write a script to find out how many integers have even number of digits.
+
+Example 1
+Input: @ints = (10, 1, 111, 24, 1000)
+Output: 3
+
+There are 3 integers having even digits i.e. 10, 24 and 1000.
+Example 2
+Input: @ints = (111, 1, 11111)
+Output: 0
+Example 3
+Input: @ints = (2, 8, 1024, 256)
+Output: 1
+=end comment
+
+my @Test =
+ # @int $exp
+ (), 0, # c/b Int
+ (1), 0,
+ (22), 1,
+ (10, 1, 111, 24, 1000), 3,
+ (111, 1, 11111), 0,
+ (2, 8, 1024, 256), 1,
+;
+plan @Test ÷ 2;
+
+sub func( $a) {
+ +@$a.grep( *.Str.chars %% 2 )
+}
+
+for @Test -> $in, $exp {
+ is func(@$in), $exp, "$exp <- $in";
+}
+
+done-testing;
+my $X = (2, 8, 1024, 256);
+
+say "\nInput: @int = @$X.raku()\nOutput: &func($X)";
+
+exit;
+
diff --git a/challenge-258/0rir/raku/ch-2.raku b/challenge-258/0rir/raku/ch-2.raku
new file mode 100644
index 0000000000..5974d7cca7
--- /dev/null
+++ b/challenge-258/0rir/raku/ch-2.raku
@@ -0,0 +1,60 @@
+#!/usr/bin/env raku
+# :vim ft=raku sw=4 expandtab # 🦋 ∅∪∩∋∈∉ ≡ ≢ «␤ » ∴
+use v6;
+use Test;
+
+=begin comment
+258-2: Sum of Values Submitted by: Mohammad Sajid Anwar
+You are given an array of integers, @int and an integer $k.
+
+Write a script to find the sum of values whose index binary representation has exactly $k number of 1-bit set.
+
+Example 1
+Input: @ints = (2, 5, 9, 11, 3), $k = 1
+Output: 17
+
+Binary representation of index 0 = 0
+Binary representation of index 1 = 1
+Binary representation of index 2 = 10
+Binary representation of index 3 = 11
+Binary representation of index 4 = 100
+
+So the indices 1, 2 and 4 have total one 1-bit sets.
+Therefore the sum, $ints[1] + $ints[2] + $ints[4] = 17
+Example 2
+Input: @ints = (2, 5, 9, 11, 3), $k = 2
+Output: 11
+Example 3
+Input: @ints = (2, 5, 9, 11, 3), $k = 0
+Output: 2
+
+=end comment
+
+my @Test =
+ # @int k exp
+ (2, 5, 9, 11, 3), 1, 17,
+ (2, 5, 9, 11, 3), 2, 11,
+ (2, 5, 9, 11, 3), 0, 2,
+ (2, 5, 9, 11, 3), 3, 0,
+;
+
+plan @Test ÷ 3;
+
+multi func( $k where * == 0, @a ) { @a[0] }
+
+multi func( $k, @a ) {
+ [+] @a[ ^@a .grep( $k == *.base(2).comb.grep('1')) ];
+}
+
+for @Test -> @in, $k, $exp {
+ is func($k, @in), $exp, "$exp\t<- $k K\t@in[]";
+}
+
+done-testing;
+my @int = ( 0..25);
+my $k = 4;
+
+say "\nInput: @int = @int.raku(), \$k = $k\nOutput: &func($k, @int)";
+
+exit;
+