diff options
| author | E. Choroba <choroba@matfyz.cz> | 2024-02-26 10:04:08 +0100 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2024-02-26 10:04:08 +0100 |
| commit | b21aeade81bb0eef51d9498abd150e3befb462a0 (patch) | |
| tree | 1e7f831eeb3e299c20afcc20c4fb929633881b8c | |
| parent | 4416b8cd33659c6d380e3ea2c5b3e21e4a861a99 (diff) | |
| download | perlweeklychallenge-club-b21aeade81bb0eef51d9498abd150e3befb462a0.tar.gz perlweeklychallenge-club-b21aeade81bb0eef51d9498abd150e3befb462a0.tar.bz2 perlweeklychallenge-club-b21aeade81bb0eef51d9498abd150e3befb462a0.zip | |
Solve 258: Count Even Digits Number & Sum of Values by E. Choroba
| -rwxr-xr-x | challenge-258/e-choroba/perl/ch-1.pl | 14 | ||||
| -rwxr-xr-x | challenge-258/e-choroba/perl/ch-2.pl | 17 |
2 files changed, 31 insertions, 0 deletions
diff --git a/challenge-258/e-choroba/perl/ch-1.pl b/challenge-258/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..f31fb68f7f --- /dev/null +++ b/challenge-258/e-choroba/perl/ch-1.pl @@ -0,0 +1,14 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +sub count_even_digits_number(@ints) { + scalar grep 0 == length() % 2, @ints +} + +use Test::More tests => 3; + +is count_even_digits_number(10, 1, 111, 24, 1000), 3, 'Example 1'; +is count_even_digits_number(111, 1, 11111), 0, 'Example 2'; +is count_even_digits_number(2, 8, 1024, 256), 1, 'Example 3'; diff --git a/challenge-258/e-choroba/perl/ch-2.pl b/challenge-258/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..5740dcd6b2 --- /dev/null +++ b/challenge-258/e-choroba/perl/ch-2.pl @@ -0,0 +1,17 @@ +#!/usr/bin/perl +use warnings; +use strict; +use experimental qw( signatures ); + +use List::Util qw{ sum }; + +sub sum_of_values($ints, $k) { + # See perldoc -f unpack for this one. + sum(@$ints[ grep $k == unpack('%32b*', pack N => $_), 0 .. $#$ints ]) +} + +use Test::More tests => 3; + +is sum_of_values([2, 5, 9, 11, 3], 1), 17, 'Example 1'; +is sum_of_values([2, 5, 9, 11, 3], 2), 11, 'Example 2'; +is sum_of_values([2, 5, 9, 11, 3], 0), 2, 'Example 3'; |
