diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-02-27 11:36:21 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-27 11:36:21 +0000 |
| commit | 54f271123393bf79cdc334f55179ae15531842ea (patch) | |
| tree | 67890691a892469dd994e3af24a3eb19c5bec482 | |
| parent | 6f45ee9e9bbcb111263339affde38539c3a86051 (diff) | |
| parent | 0e59e25786af5ec1b483205e48a66a7257d7f934 (diff) | |
| download | perlweeklychallenge-club-54f271123393bf79cdc334f55179ae15531842ea.tar.gz perlweeklychallenge-club-54f271123393bf79cdc334f55179ae15531842ea.tar.bz2 perlweeklychallenge-club-54f271123393bf79cdc334f55179ae15531842ea.zip | |
Merge pull request #9657 from jacoby/master
DAJ 258
| -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; +} |
