diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-03-04 00:21:07 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2024-03-04 00:21:07 +0000 |
| commit | 6047ae2284ecc043eadaf4ccbd11d16ac0dd27e1 (patch) | |
| tree | 73c2632aab4a14833a0094441c1ee66be1ef0459 | |
| parent | 40d47c8ead708860c4e3f21bae091506a42807f4 (diff) | |
| parent | 4597936d06c260bebd4f5922abc56c1067fc504f (diff) | |
| download | perlweeklychallenge-club-6047ae2284ecc043eadaf4ccbd11d16ac0dd27e1.tar.gz perlweeklychallenge-club-6047ae2284ecc043eadaf4ccbd11d16ac0dd27e1.tar.bz2 perlweeklychallenge-club-6047ae2284ecc043eadaf4ccbd11d16ac0dd27e1.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
21 files changed, 484 insertions, 93 deletions
diff --git a/challenge-258/adam-russell/blog.txt b/challenge-258/adam-russell/blog.txt new file mode 100644 index 0000000000..f463d23f1e --- /dev/null +++ b/challenge-258/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2024/03/03
\ No newline at end of file diff --git a/challenge-258/adam-russell/perl/ch-1.pl b/challenge-258/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..b176d1cbbd --- /dev/null +++ b/challenge-258/adam-russell/perl/ch-1.pl @@ -0,0 +1,19 @@ + + +use v5.38; + + +sub count_even_digits{ + return 0 + + grep { + my $x = $_; $x =~ tr/[0-9]//d % 2 == 0 + } @_; +} + + +MAIN:{ + say count_even_digits 10, 1, 111, 24, 1000; + say count_even_digits 111, 1, 11111; + say count_even_digits 2, 8, 1024, 256; +} + diff --git a/challenge-258/adam-russell/perl/ch-2.pl b/challenge-258/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..325cdd8cd4 --- /dev/null +++ b/challenge-258/adam-russell/perl/ch-2.pl @@ -0,0 +1,34 @@ + + +use v5.38; + + +sub count_bits{ + my($x) = @_; + my $total_count_set_bit = 0; + while($x){ + my $b = $x & 1; + $total_count_set_bit++ if $b; + $x = $x >> 1; + } + return $total_count_set_bit; +} + + +sub sum_of_values{ + my $k = shift; + my(@n) = @_; + my $sum; + do{ + $sum += $_[$_] if count_bits($_) == $k; + } for 0 .. @n - 1; + return $sum; +} + + +MAIN:{ + say sum_of_values 1, 2, 5, 9, 11, 3; + say sum_of_values 2, 2, 5, 9, 11, 3; + say sum_of_values 0, 2, 5, 9, 11, 3; +} + diff --git a/challenge-258/barroff/blog.txt b/challenge-258/barroff/blog.txt new file mode 100644 index 0000000000..f7fc201613 --- /dev/null +++ b/challenge-258/barroff/blog.txt @@ -0,0 +1 @@ +https://barroff.codeberg.page/2024/03/all-about-digits/ diff --git a/challenge-258/barroff/bqn/ch-1.bqn b/challenge-258/barroff/bqn/ch-1.bqn new file mode 100644 index 0000000000..6162da09e0 --- /dev/null +++ b/challenge-258/barroff/bqn/ch-1.bqn @@ -0,0 +1,7 @@ +#/usr/bin/env bqn + +CountEvenDigitsNumber ← +´∘(¬2|≠•Fmt)¨ + +•Show CountEvenDigitsNumber 10‿1‿111‿24‿1000 +•Show CountEvenDigitsNumber 111‿1‿11111 +•Show CountEvenDigitsNumber 2‿8‿1024‿256 diff --git a/challenge-258/barroff/bqn/ch-2.bqn b/challenge-258/barroff/bqn/ch-2.bqn new file mode 100644 index 0000000000..4164500fca --- /dev/null +++ b/challenge-258/barroff/bqn/ch-2.bqn @@ -0,0 +1,11 @@ +#/usr/bin/env bqn + +ints ← 2‿5‿9‿11‿3 + +DecToBin ← { (((0⊸<)◶""‿𝕊∘⌊2⊸(÷˜))∾(•Fmt 2⊸|)) 𝕩 } + +SumOfValues ← { +´𝕩×𝕨=+´¨('0'⊸(-˜)¨)¨DecToBin¨↕≠𝕩 } + +•Show 1 SumOfValues ints +•Show 2 SumOfValues ints +•Show 0 SumOfValues ints diff --git a/challenge-258/barroff/julia/ch-1.jl b/challenge-258/barroff/julia/ch-1.jl new file mode 100644 index 0000000000..2e1969a2f7 --- /dev/null +++ b/challenge-258/barroff/julia/ch-1.jl @@ -0,0 +1,14 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function count_even_digits_number(ints::Vector{T}) where {T<:Integer} + length(filter(x -> lastindex("$x") % 2 == 0, ints)) +end + +@testset "count even digits number" begin + @test count_even_digits_number([10, 1, 111, 24, 1000]) == 3 + @test count_even_digits_number([111, 1, 11111]) == 0 + @test count_even_digits_number([2, 8, 1024, 256]) == 1 +end + diff --git a/challenge-258/barroff/julia/ch_2.jl b/challenge-258/barroff/julia/ch_2.jl new file mode 100644 index 0000000000..e322ee3845 --- /dev/null +++ b/challenge-258/barroff/julia/ch_2.jl @@ -0,0 +1,20 @@ +#!/usr/bin/env julia + +using Test: @test, @testset + +function sum_of_values(ints::Vector{T}, k::T) where {T<:Integer} + sum( + ints[ + filter( + x -> count(y -> y == '1', string(x - 1, base=2)) == k, + 1:length(ints) + ) + ] + ) +end + +@testset "count even digits number" begin + @test sum_of_values([2, 5, 9, 11, 3], 1) == 17 + @test sum_of_values([2, 5, 9, 11, 3], 2) == 11 + @test sum_of_values([2, 5, 9, 11, 3], 0) == 2 +end 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); +} diff --git a/challenge-258/bruce-gray/java/ch-1.java b/challenge-258/bruce-gray/java/ch-1.java new file mode 100644 index 0000000000..b60b8fcdf4 --- /dev/null +++ b/challenge-258/bruce-gray/java/ch-1.java @@ -0,0 +1,34 @@ +// java java/ch-1.java +import java.util.List; +public class ch_1 { + + + static long task1 ( List<Integer> ns ) { + return ns.stream() + .filter(n -> n.toString().length() % 2 == 0) + .count(); + } + + + // Test harness + static int test_number = 1; + static void plan ( Integer count ) { System.out.printf("1..%d\n", count); } + static boolean is ( long got, long expected, String desc ) { + boolean ret = got == expected; + System.out.printf("%s %d - %s\n", (ret ? "ok" : "not ok"), test_number++, desc); + return ret; + } + public static void main(String[] args) { + record Testcase ( int expected, List<Integer> inputs ) {} + + List<Testcase> tst = List.of( + new Testcase( 3, List.of( 10, 1, 111, 24, 1000 ) ), + new Testcase( 0, List.of( 111, 1, 11111 ) ), + new Testcase( 1, List.of( 2, 8, 1024, 256 ) ) + ); + plan(tst.size()); + for (Testcase t : tst) { + is( task1(t.inputs), t.expected, t.inputs.toString() ); + } + } +} diff --git a/challenge-258/bruce-gray/java/ch-2.java b/challenge-258/bruce-gray/java/ch-2.java new file mode 100644 index 0000000000..f7f9036d89 --- /dev/null +++ b/challenge-258/bruce-gray/java/ch-2.java @@ -0,0 +1,41 @@ +// java java/ch-2.java +import java.util.List; +import java.util.stream.IntStream; +import java.util.stream.Collectors; +public class ch_2 { + + + static int task2 ( Integer k, List<Integer> ns ) { + Integer[] ints = ns.toArray(new Integer[0]); + + return IntStream.range(0, ints.length) + .filter(i -> Integer.bitCount(i) == k) + .mapToObj(i -> ints[i]) + .collect(Collectors.summingInt(Integer::intValue)); + } + + + + // Test harness + static int test_number = 1; + static void plan ( Integer count ) { System.out.printf("1..%d\n", count); } + static boolean is ( Integer got, Integer expected, String desc ) { + boolean ret = got == expected; + System.out.printf("%s %d - %s\n", (ret ? "ok" : "not ok"), test_number++, desc); + return ret; + } + public static void main(String[] args) { + record Testcase ( Integer expected, Integer in_k, List<Integer> in_nums ) {} + + List<Testcase> tst = List.of( + new Testcase( 17, 1, List.of(2, 5, 9, 11, 3) ), + new Testcase( 11, 2, List.of(2, 5, 9, 11, 3) ), + new Testcase( 2, 0, List.of(2, 5, 9, 11, 3) ) + ); + plan(tst.size()); + for (Testcase t : tst) { + is( task2(t.in_k, t.in_nums), t.expected, "k=" + t.in_k); + } + + } +} diff --git a/challenge-258/bruce-gray/perl/ch-1.pl b/challenge-258/bruce-gray/perl/ch-1.pl new file mode 100644 index 0000000000..98ab3129d5 --- /dev/null +++ b/challenge-258/bruce-gray/perl/ch-1.pl @@ -0,0 +1,16 @@ +use v5.36; +sub task1 ( @ns ) { + return 0+grep { length(abs $_) % 2 == 0 } @ns; +} + + +my @tests = ( + [ 3, [ 10, 1, 111, 24, 1000 ] ], + [ 0, [ 111, 1, 11111 ] ], + [ 1, [ 2, 8, 1024, 256 ] ], +); +use Test::More; plan tests => 0+@tests; +for (@tests) { + my ($expected, $in) = @{$_}; + is task1(@{$in}), $expected; +} diff --git a/challenge-258/bruce-gray/perl/ch-2.pl b/challenge-258/bruce-gray/perl/ch-2.pl new file mode 100644 index 0000000000..851d62d479 --- /dev/null +++ b/challenge-258/bruce-gray/perl/ch-2.pl @@ -0,0 +1,22 @@ +use v5.36; +use List::Util qw<sum>; + +sub pop_count ($n) { sprintf( '%b', $n ) =~ tr/1// } + +sub task2 ( $k, @ns ) { + my @wanted_keys = grep { pop_count($_) == $k } keys @ns; + + return sum @ns[ @wanted_keys ]; +} + + +my @tests = ( + [ 17, 1, [2, 5, 9, 11, 3] ], + [ 11, 2, [2, 5, 9, 11, 3] ], + [ 2, 0, [2, 5, 9, 11, 3] ], +); +use Test::More; plan tests => 0+@tests; +for (@tests) { + my ($expected, $in_k, $in_ints) = @{$_}; + is task2($in_k, @{$in_ints}), $expected; +} diff --git a/challenge-258/bruce-gray/raku/ch-1.raku b/challenge-258/bruce-gray/raku/ch-1.raku new file mode 100644 index 0000000000..c11d9222a8 --- /dev/null +++ b/challenge-258/bruce-gray/raku/ch-1.raku @@ -0,0 +1,13 @@ +sub task1 ( @ns --> UInt ) { + return +grep { .abs.chars %% 2 }, @ns; +} + + +use Test; plan +my @tests = + ( 3, ( 10, 1, 111, 24, 1000 ) ), + ( 0, ( 111, 1, 11111 ) ), + ( 1, ( 2, 8, 1024, 256 ) ), +; +for @tests -> ( $expected, @in ) { + is task1(@in), $expected; +} diff --git a/challenge-258/bruce-gray/raku/ch-2.raku b/challenge-258/bruce-gray/raku/ch-2.raku new file mode 100644 index 0000000000..e20d65b1b3 --- /dev/null +++ b/challenge-258/bruce-gray/raku/ch-2.raku @@ -0,0 +1,33 @@ +sub pop-count { $^n.base(2).comb('1').elems } + +sub pop-count-fast ($n is copy) { + constant @bits_per_2bytes = [X+] (0,1) xx 16; # Bwaahaahaa! + my $c = 0; + while $n { + $c += @bits_per_2bytes[$n +& 0xFFFF]; + $n +>= 16; + } + return $c; +} + +sub task2 ( UInt $k, @ns --> UInt ) { + # More concise alternatives: + # return @ns.grep({ pop-count($++) == $k }).sum; + # return @ns.pairs.map({ .value if pop-count(.key) == $k }).sum; + + my &pop-count-is-k = *.&pop-count == $k; + + my @wanted_keys = @ns.keys.grep: &pop-count-is-k; + + return @ns[ @wanted_keys ].sum; +} + + +use Test; plan +my @tests = + ( 17, 1, (2, 5, 9, 11, 3) ), + ( 11, 2, (2, 5, 9, 11, 3) ), + ( 2, 0, (2, 5, 9, 11, 3) ), +; +for @tests -> ( $expected, $in_k, @in_ints ) { + is task2($in_k, @in_ints), $expected; +} diff --git a/challenge-258/matthias-muth/README.md b/challenge-258/matthias-muth/README.md index e483f78d49..05da216c90 100644 --- a/challenge-258/matthias-muth/README.md +++ b/challenge-258/matthias-muth/README.md @@ -1,126 +1,88 @@ -# Challenge 256 tasks: Easy Pairs - Easy Merge -**Challenge 256 solutions in Perl by Matthias Muth** +# You Only Grep Twice -## Task 1: Maximum Pairs +**Challenge 258 solutions in Perl by Matthias Muth** -> You are given an array of distinct words, `@words`.<br/> -> Write a script to find the maximum pairs in the given array. The words `$words[i]` and `$words[j]` can be a pair one is reverse of the other.<br/> +## Task 1: Count Even Digits Number + +> You are given a array of positive integers, @ints.<br/> +> Write a script to find out how many integers have even number of digits.<br/> > <br/> > Example 1<br/> -> Input: @words = ("ab", "de", "ed", "bc")<br/> -> Output: 1<br/> -> There is one pair in the given array: "de" and "ed"<br/> +> Input: @ints = (10, 1, 111, 24, 1000)<br/> +> Output: 3<br/> +> There are 3 integers having even digits i.e. 10, 24 and 1000.<br/> > <br/> > Example 2<br/> -> Input: @words = ("aa", "ba", "cd", "ed")<br/> +> Input: @ints = (111, 1, 11111)<br/> > Output: 0<br/> > <br/> > Example 3<br/> -> Input: @words = ("uv", "qp", "st", "vu", "mn", "pq"))<br/> -> Output: 2<br/> +> Input: @ints = (2, 8, 1024, 256)<br/> +> Output: 1<br/> -Ah, an easy one.<br/> -For each word we check whether we have seen its reverse before, and increment -our counter if so. Then we remember that we have seen the current word. +This is a small exercise for using `grep`.<br/> +We go through all values in the `@ints` array. For each value, we get the number of digits using the `length` function. Perl's type flexibility and implicit type conversion helps us here, in that the `length` returns the number of characters of the current value *as a string*. -Perl supports us with the `reverse` function to reverse the characters of a string (also to reverse a list, but this is not what we use here). +We then check whether this number is even, using this as a filter for `grep`. -```perl -#!/usr/bin/env perl -use v5.36; +When called in scalar context, `grep` returns the number of values that fulfill the criteria, not the list of values itself. As we are only interested in this number, we call `grep` in scalar context explicitly, and we are done. -sub maximum_pairs( @words ) { - my $n = 0; - my %known; - for ( @words ) { - ++$n if $known{ reverse $_ }; - $known{$_} = 1; - } - return $n; -} - -use Test2::V0 qw( -no_srand ); -is maximum_pairs( "ab", "de", "ed", "bc" ), 1, - 'Example 1: maximum_pairs( ("ab", "de", "ed", "bc") ) == 1'; -is maximum_pairs( "aa", "ba", "cd", "ed" ), 0, - 'Example 2: maximum_pairs( ("aa", "ba", "cd", "ed") ) == 0'; -is maximum_pairs( "uv", "qp", "st", "vu", "mn", "pq" ), 2, - 'Example 3: maximum_pairs( ("uv", "qp", "st", "vu", "mn", "pq") ) == 2'; -done_testing; -``` - -If you don't have perl 5.36 (which I highly recommend!), you can use this -instead: +Long explanation, short code: ```perl -use v5.20; -use warnings; -use feature 'signatures'; -no warnings 'experimental::signatures'; +use v5.36; +sub count_even_digits_number( @ints ) { + return scalar grep length( $_ ) % 2 == 0, @ints; +} ``` -Perl 5.20 has been around since 2014, so I guess that chances are high -that your perl is more recent than that.<br/> -If not, and you are not able to update your system's perl for any reason, -I suggest installing [`perlbrew`](https://perlbrew.pl), -which is an admin-free perl installation management tool. -## Task 2: Merge Strings +## Task 2: Sum of Values -> You are given two strings, `$str1` and `$str2`.<br/> -> Write a script to merge the given strings by adding in alternative order starting with the first string. If a string is longer than the other then append the remaining at the end.<br/> +> You are given an array of integers, @int and an integer \$k.<br/> +> Write a script to find the sum of values whose index binary representation has exactly \$k number of 1-bit set.<br/> > <br/> > Example 1<br/> -> Input: \$str1 = "abcd", \$str2 = "1234"<br/> -> Output: "a1b2c3d4"<br/> +> Input: @ints = (2, 5, 9, 11, 3), \$k = 1<br/> +> Output: 17<br/> +> Binary representation of index 0 = 0<br/> +> Binary representation of index 1 = 1<br/> +> Binary representation of index 2 = 10<br/> +> Binary representation of index 3 = 11<br/> +> Binary representation of index 4 = 100<br/> +> So the indices 1, 2 and 4 have total one 1-bit sets.<br/> +> Therefore the sum, \$ints[1] + \$ints[2] + \$ints[3] = 17<br/> > <br/> > Example 2<br/> -> Input: \$str1 = "abc", \$str2 = "12345"<br/> -> Output: "a1b2c345"<br/> +> Input: @ints = (2, 5, 9, 11, 3), \$k = 2<br/> +> Output: 11<br/> > <br/> > Example 3<br/> -> Input: \$str1 = "abcde", \$str2 = "123"<br/> -> Output: "a1b2c3de"<br/> - -The idea for this challenge is to turn the two string into lists of characters, -and then merge the two lists.<br/> -There are a lot of functions for list manipulations in the `List::Util` core -module, one of which is `mesh`. It does exactly what we need. -The only downside is that if the lists are of different lengths, there will be -`undef` values inserted in the result. But it is easy to `grep` those out -before assembling the result into a return string. -At least easier than splitting up the longer list into two parts, and after -`mesh`ing the first part with the shorter string appending the second part. - -`mesh` has been part of `List::Util` since its version 1.56, -which means has been part of standard Perl since Perl 5.25 (released in 2014). - -Its implementation is different from the `mesh` function in the -`List::MoreUtils` CPAN module, in that it uses array references as parameters, not array variables that are used *by reference* (using prototypes).<br/> -I prefer the `List::Util` version here, -because we can directly use anonymous arrays containing the split characters as parameters, -making it unnecessary to declare and use any array variables. +> Input: @ints = (2, 5, 9, 11, 3), \$k = 0<br/> +> Output: 2<br/> -```perl -#!/usr/bin/env perl +This looks very similar. We also can use `grep`to select the values that we process further.<br/> +But after reading the task again, and also checking the examples, we notice that in the filter, we don't use the numbers in the array, but the *index* of the numbers. -use v5.36; +So what we do is to use `grep` again, but this time, we go through all indexes instead of the values, filtering on the number of 1-bits that the index has, and then `map`the filtered indexes back to their array values. Then we can `sum` up those values. -use List::Util qw( mesh ); +For getting the number of 1-bits in an integer number, probably the most efficient way is to use `unpack` with a `'%'` field prefix to do a checksum of the packed binary representation of our number (see examples in [perldoc](https://perldoc.perl.org/functions/unpack)). So for numbers at least up to 32 bits, this function does a marvelous job: -sub merge_strings( $str1, $str2 ) { - return join "", - grep defined, - mesh [ split //, $str1 ], [ split //, $str2 ]; +```perl +use v5.36; +sub n_bits( $n ) { + return unpack "%b*", pack "i", $n; } +``` + +(`use v5.36;` is the shortest way to get function prototypes, which I don't want to miss.) + +The rest is straightforward: -use Test2::V0 qw( -no_srand ); -is merge_strings( "abcd", 1234 ), "a1b2c3d4", - 'Example 1: merge_strings( ("abcd", 1234) ) == "a1b2c3d4"'; -is merge_strings( "abc", 12345 ), "a1b2c345", - 'Example 2: merge_strings( ("abc", 12345) ) == "a1b2c345"'; -is merge_strings( "abcde", 123 ), "a1b2c3de", - 'Example 3: merge_strings( ("abcde", 123) ) == "a1b2c3de"'; -done_testing; +```perl +use List::Util qw( sum ); +sub sum_of_values( $ints, $k ) { + return sum map $ints->[$_], grep n_bits( $_ ) == $k, 0..$ints->$#*; +} ``` #### **Thank you for the challenge!** diff --git a/challenge-258/matthias-muth/blog.txt b/challenge-258/matthias-muth/blog.txt new file mode 100644 index 0000000000..50c90fa6ea --- /dev/null +++ b/challenge-258/matthias-muth/blog.txt @@ -0,0 +1 @@ +https://github.com/MatthiasMuth/perlweeklychallenge-club/tree/muthm-258/challenge-258/matthias-muth#readme diff --git a/challenge-258/matthias-muth/perl/ch-1.pl b/challenge-258/matthias-muth/perl/ch-1.pl new file mode 100755 index 0000000000..860860861b --- /dev/null +++ b/challenge-258/matthias-muth/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/env perl +# +# The Weekly Challenge - Perl & Raku +# (https://theweeklychallenge.org) +# +# Challenge 258 Task 1: Count Even Digits Number +# +# Perl solution by Matthias Muth. +# + +use v5.36; + +sub count_even_digits_number( @ints ) { + return scalar grep length( $_ ) % 2 == 0, @ints; +} + +use Test2::V0 qw( -no_srand ); +is count_even_digits_number( 10, 1, 111, 24, 1000 ), 3, + 'Example 1: count_even_digits_number( 10, 1, 111, 24, 1000 ) == 3'; +is count_even_digits_number( 111, 1, 11111 ), 0, + 'Example 2: count_even_digits_number( 111, 1, 11111 ) == 0'; +is count_even_digits_number( 2, 8, 1024, 256 ), 1, + 'Example 3: count_even_digits_number( 2, 8, 1024, 256 ) == 1'; +done_testing; diff --git a/challenge-258/matthias-muth/perl/ch-2.pl b/challenge-258/matthias-muth/perl/ch-2.pl new file mode 100755 index 0000000000..a46bb18134 --- /dev/null +++ b/challenge-258/matthias-muth/perl/ch-2.pl @@ -0,0 +1,30 @@ +#!/usr/bin/env perl +# +# The Weekly Challenge - Perl & Raku +# (https://theweeklychallenge.org) +# +# Challenge 258 Task 2: Sum of Values +# +# Perl solution by Matthias Muth. +# + +use v5.36; + +use List::Util qw( sum ); + +sub n_bits( $n ) { + return unpack "%b*", pack "i", $n; +} + +sub sum_of_values( $ints, $k ) { + return sum map $ints->[$_], grep n_bits( $_ ) == $k, 0..$ints->$#*; +} + +use Test2::V0 qw( -no_srand ); +is sum_of_values( [2, 5, 9, 11, 3], 1 ), 17, + 'Example 1: sum_of_values( [2, 5, 9, 11, 3], 1 ) == 17'; +is sum_of_values( [2, 5, 9, 11, 3], 2 ), 11, + 'Example 2: sum_of_values( [2, 5, 9, 11, 3], 2 ) == 11'; +is sum_of_values( [2, 5, 9, 11, 3], 0 ), 2, + 'Example 3: sum_of_values( [2, 5, 9, 11, 3], 0 ) == 2'; +done_testing; diff --git a/challenge-258/matthias-muth/perl/challenge-258.txt b/challenge-258/matthias-muth/perl/challenge-258.txt new file mode 100644 index 0000000000..ba1f16cedd --- /dev/null +++ b/challenge-258/matthias-muth/perl/challenge-258.txt @@ -0,0 +1,58 @@ +The Weekly Challenge - 258 +Monday, Feb 26, 2024 + + +Task 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 + + +Task 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 + + +Last date to submit the solution 23:59 (UK Time) Sunday 3rd March 2024. |
