diff options
| author | rir <rirans@comcast.net> | 2024-03-02 22:31:16 -0500 |
|---|---|---|
| committer | rir <rirans@comcast.net> | 2024-03-02 22:31:16 -0500 |
| commit | 0437ee5479926b86b3efa67990764b02d17667fa (patch) | |
| tree | a36a65d4192860bc35cdd5c8cdf97bb2ad64b074 /challenge-258 | |
| parent | 3d74bbdcd76c01caf50ee28425bf635326793163 (diff) | |
| download | perlweeklychallenge-club-0437ee5479926b86b3efa67990764b02d17667fa.tar.gz perlweeklychallenge-club-0437ee5479926b86b3efa67990764b02d17667fa.tar.bz2 perlweeklychallenge-club-0437ee5479926b86b3efa67990764b02d17667fa.zip | |
257 258
Diffstat (limited to 'challenge-258')
| -rw-r--r-- | challenge-258/0rir/raku/ch-1.raku | 50 | ||||
| -rw-r--r-- | challenge-258/0rir/raku/ch-2.raku | 60 |
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; + |
