diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2024-02-26 11:55:40 -0500 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2024-02-26 11:55:40 -0500 |
| commit | 42985187d08b013efc571821618c13ac8ca70076 (patch) | |
| tree | 3071f36d33e19d6639ac1ba203a0de1f134c51cb | |
| parent | 4416b8cd33659c6d380e3ea2c5b3e21e4a861a99 (diff) | |
| download | perlweeklychallenge-club-42985187d08b013efc571821618c13ac8ca70076.tar.gz perlweeklychallenge-club-42985187d08b013efc571821618c13ac8ca70076.tar.bz2 perlweeklychallenge-club-42985187d08b013efc571821618c13ac8ca70076.zip | |
258 DAJ
| -rw-r--r-- | challenge-258/dave-jacoby/perl/ch-1.pl | 23 | ||||
| -rw-r--r-- | challenge-258/dave-jacoby/perl/ch-2.pl | 47 |
2 files changed, 70 insertions, 0 deletions
diff --git a/challenge-258/dave-jacoby/perl/ch-1.pl b/challenge-258/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..e9152964ba --- /dev/null +++ b/challenge-258/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +my @examples = ( + + [ 10, 1, 111, 24, 1000 ], + [ 111, 1, 11111 ], + [ 2, 8, 1024, 256 ], +); + +for my $example (@examples) { + my $output = scalar grep { ( length $_ ) % 2 == 0 } $example->@*; + my $ints = join ', ', $example->@*; + + say <<~"END"; + Input: \@ints = ($ints) + Output: $output + END +} + diff --git a/challenge-258/dave-jacoby/perl/ch-2.pl b/challenge-258/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..501e50a669 --- /dev/null +++ b/challenge-258/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,47 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ sum0 }; + +my @examples = ( + + { + ints => [ 2, 5, 9, 11, 3 ], + k => 1 + }, + { + ints => [ 2, 5, 9, 11, 3 ], + k => 2 + }, + { + ints => [ 2, 5, 9, 11, 3 ], + k => 0 + } +); + +for my $example (@examples) { + my @output = sum_of_values($example); + my $ints = join ', ', $example->{ints}->@*; + my $k = join ', ', $example->{k}; + my $output = join ', ', @output; + + say <<~"END"; + Input: \@ints = ($ints), \$k = $k + Output: $output + END +} + +sub sum_of_values ($obj) { + my @ints = $obj->{ints}->@*; + my $k = $obj->{k}; + my $output = 0; + + for my $i ( 0 .. $#ints ) { + my $s = sum0 split //, sprintf '%b', $i; + $output += $ints[$i] if $s == $k; + } + return $output; +} |
