diff options
| author | pme <hauptadler@gmail.com> | 2024-02-26 17:53:49 +0100 |
|---|---|---|
| committer | pme <hauptadler@gmail.com> | 2024-02-26 17:53:49 +0100 |
| commit | 97a4959fdf2f31cea3e91447f95b719c14d8660e (patch) | |
| tree | 88a51c7aa2fab4f768206d5c50df2b36bccbfdd4 /challenge-258 | |
| parent | 4416b8cd33659c6d380e3ea2c5b3e21e4a861a99 (diff) | |
| download | perlweeklychallenge-club-97a4959fdf2f31cea3e91447f95b719c14d8660e.tar.gz perlweeklychallenge-club-97a4959fdf2f31cea3e91447f95b719c14d8660e.tar.bz2 perlweeklychallenge-club-97a4959fdf2f31cea3e91447f95b719c14d8660e.zip | |
challenge-258
Diffstat (limited to 'challenge-258')
| -rwxr-xr-x | challenge-258/peter-meszaros/perl/ch-1.pl | 47 | ||||
| -rwxr-xr-x | challenge-258/peter-meszaros/perl/ch-2.pl | 62 |
2 files changed, 109 insertions, 0 deletions
diff --git a/challenge-258/peter-meszaros/perl/ch-1.pl b/challenge-258/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..f1399beb7a --- /dev/null +++ b/challenge-258/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl +# +# 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 +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [10, 1, 111, 24, 1000], + [111, 1, 11111], + [2, 8, 1024, 256], +]; + +sub count_even_digits_number +{ + my $ints = shift; + + return scalar grep { not split('') % 2 } @$ints; +} + +is(count_even_digits_number($cases->[0]), 3, 'Example 1'); +is(count_even_digits_number($cases->[1]), 0, 'Example 2'); +is(count_even_digits_number($cases->[2]), 1, 'Example 3'); +done_testing(); + +exit 0; diff --git a/challenge-258/peter-meszaros/perl/ch-2.pl b/challenge-258/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..49e7b3c13f --- /dev/null +++ b/challenge-258/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,62 @@ +#!/usr/bin/env perl +# +# 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[3] = 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 +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; +use List::Util qw/sum0/; + +my $cases = [ + [[2, 5, 9, 11, 3], 1], + [[2, 5, 9, 11, 3], 2], + [[2, 5, 9, 11, 3], 0], +]; + +sub sum_of_values +{ + my ($l, $k) = $_[0]->@*; + + my $sum = 0; + for my $i (0..$#$l) { + my $ones = sum0(grep { $_ } split('', unpack("B32", pack("N", $i)))); + $sum += $l->[$i] if $ones == $k; + } + return $sum; +} + +is(sum_of_values($cases->[0]), 17, 'Example 1'); +is(sum_of_values($cases->[1]), 11, 'Example 2'); +is(sum_of_values($cases->[2]), 2, 'Example 3'); +done_testing(); + +exit 0; + |
