diff options
| -rw-r--r-- | challenge-258/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-258/dave-jacoby/perl/ch-1.pl | 23 | ||||
| -rw-r--r-- | challenge-258/dave-jacoby/perl/ch-2.pl | 47 |
3 files changed, 71 insertions, 0 deletions
diff --git a/challenge-258/dave-jacoby/blog.txt b/challenge-258/dave-jacoby/blog.txt new file mode 100644 index 0000000000..151213d6f6 --- /dev/null +++ b/challenge-258/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2024/02/26/no-mental-bandwidth-for-a-name-weekly-challenge-258.html 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; +} |
