diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-03-02 12:41:59 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-02 12:41:59 +0000 |
| commit | 08658e39b67558af2c8e8b1849f00ca4e38cb5dc (patch) | |
| tree | a41ebaa1a52d4a3db18981a26678cae194d64d19 | |
| parent | d9b3f6be44bf5cdfd4b2cdc3a4e60f96285e2dbe (diff) | |
| parent | 4a17112973c485e46e8ad58ccc9bfef12abf26c3 (diff) | |
| download | perlweeklychallenge-club-08658e39b67558af2c8e8b1849f00ca4e38cb5dc.tar.gz perlweeklychallenge-club-08658e39b67558af2c8e8b1849f00ca4e38cb5dc.tar.bz2 perlweeklychallenge-club-08658e39b67558af2c8e8b1849f00ca4e38cb5dc.zip | |
Merge pull request #9678 from jo-37/contrib
Solutions to challenge 258
| -rw-r--r-- | challenge-258/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-258/jo-37/perl/ch-1.pl | 62 | ||||
| -rwxr-xr-x | challenge-258/jo-37/perl/ch-2.pl | 60 |
3 files changed, 123 insertions, 0 deletions
diff --git a/challenge-258/jo-37/blog.txt b/challenge-258/jo-37/blog.txt new file mode 100644 index 0000000000..f757f71bbc --- /dev/null +++ b/challenge-258/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2024/03/01/ch-258.html diff --git a/challenge-258/jo-37/perl/ch-1.pl b/challenge-258/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..510091a56d --- /dev/null +++ b/challenge-258/jo-37/perl/ch-1.pl @@ -0,0 +1,62 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use Math::Prime::Util 'todigits'; + +our ($tests, $examples, $base); +$base ||= 10; + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [-base=BASE] [N...] + +-examples + run the examples from the challenge + +-tests + run some tests + +-verbose + enable trace output + +-base=BASE + regard numbers in base BASE. Default: 10 + +N... + list of numbers + +EOS + + +### Input and Output + +say cedn($base, @ARGV); + + +### Implementation + +sub cedn { + my $base = shift; + scalar grep !(scalar(todigits($_, $base)) % 2), @_ +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is cedn(10 => 10, 1, 111, 24, 1000), 3, 'example 1'; + is cedn(10 => 111, 1, 11111), 0, 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} diff --git a/challenge-258/jo-37/perl/ch-2.pl b/challenge-258/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..c9bf4e62d7 --- /dev/null +++ b/challenge-258/jo-37/perl/ch-2.pl @@ -0,0 +1,60 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use List::Util 'sum0'; + +our ($tests, $examples, $bits); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless defined $bits and @ARGV; +usage: $0 [-examples] [-tests] [-bits=K] [N...] + +-examples + run the examples from the challenge + +-tests + run some tests + +-bits=K + sum over numbers with a k-bit index + +N... + list of numbers + +EOS + + +### Input and Output + +say k_bit_sum($bits, @ARGV); + + +### Implementation + +sub k_bit_sum { + my $k = shift; + + sum0 @_[grep unpack('%32b*', pack('l', $_)) == $k, 0 .. $#_]; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is k_bit_sum(1 ,=> 2, 5, 9, 11, 3), 17, 'example 1'; + is k_bit_sum(2 ,=> 2, 5, 9, 11, 3), 11, 'example 2'; + is k_bit_sum(0 ,=> 2, 5, 9, 11, 3), 2, 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} |
