## Perl Weekly Challenge 258: Count Even Digits Numbers

These are some answers to the Week 258, Task 1, of the Perl Weekly Challenge organized by Mohammad S. Anwar.

*Spoiler Alert:* This weekly challenge deadline is due in a few days from now (on March 3, 2024 at 23:59). This blog post provides some solutions to this challenge. Please don’t read on if you intend to complete the challenge on your own.

## Task 1: Count Even Digits Number

*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
```

### Count Even Digits Number in Raku

We use the `chars`

function to count the characters of each integer, `grep`

with the `%`

modulo oprator to filter the counts that are even, and finally the `elems`

method to count the integers satisfying the desired condition. Altogether, a nice little oner-liner.

```
sub count-even-digits-ints (@in) {
(grep { .chars %% 2 }, @in).elems;
}
my @tests = <10 1 111 24 1000>, <111 1 11111>, <2 8 1024 256>;
for @tests -> @test {
printf "%-20s => ", "@test[]";
say count-even-digits-ints @test;
}
```

This program displays the following output:

```
$ raku ./count-even-digits.raku
10 1 111 24 1000 => 3
111 1 11111 => 0
2 8 1024 256 => 1
```

### Count Even Digits Number in Perl

This is a port to Perl of the above Raku program, using `scalar`

and `length`

to replace `elems`

and `chars`

. Also a concise one-liner.

```
use strict;
use warnings;
use feature 'say';
sub count_even_digits_ints {
scalar grep { ! (length($_) % 2) } @_;
}
my @tests = ( [<10 1 111 24 1000>],
[<111 1 11111>], [<2 8 1024 256>] );
for my $test (@tests) {
printf "%-20s => ", "@$test";
say count_even_digits_ints @$test;
}
```

This program displays the following output:

```
$ perl ./count-even-digits.pl
10 1 111 24 1000 => 3
111 1 11111 => 0
2 8 1024 256 => 1
```

### Count Even Digits Number in Julia

Again, a port of the two previous programs to Julia. The only significant difference is that we need to explicitly convert integers to strings to be able to find their length (number of characters).

```
using Printf
function count_even_digits_ints(invals)
evens = filter(x -> (mod(length(string(x)), 2 ) == 0), invals)
return size(evens, 1)
end
tests = [ [100, 1, 111, 424, 1000],
[111, 1, 11111], [2, 8, 1024, 256] ]
for test in tests
@printf "%-25s => " "$test"
println("$(count_even_digits_ints(test))")
end
```

This program displays the following output:

```
$ julia count-even-digits.jl
[100, 1, 111, 424, 1000] => 1
[111, 1, 11111] => 0
[2, 8, 1024, 256] => 1
```

## Wrapping up

The next week Perl Weekly Challenge will start soon. If you want to participate in this challenge, please check https://perlweeklychallenge.org/ and make sure you answer the challenge before 23:59 BST (British summer time) on March 10, 2024. And, please, also spread the word about the Perl Weekly Challenge if you can.

## Leave a comment