diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-05-18 23:22:29 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-18 23:22:29 +0100 |
| commit | 8d1db242e6d28eb45e7fc8963488eee71f687d7b (patch) | |
| tree | 58623f06fca756b4e6dc163ef169ef9dc53fdde4 | |
| parent | fdd10667f8e6d37006f1e0931b16eaf2e073ca86 (diff) | |
| parent | a70a775c5394e20599766ef7dcaa189beaee10ae (diff) | |
| download | perlweeklychallenge-club-8d1db242e6d28eb45e7fc8963488eee71f687d7b.tar.gz perlweeklychallenge-club-8d1db242e6d28eb45e7fc8963488eee71f687d7b.tar.bz2 perlweeklychallenge-club-8d1db242e6d28eb45e7fc8963488eee71f687d7b.zip | |
Merge pull request #12036 from jo-37/contrib
Solutions to challenge 321
| -rw-r--r-- | challenge-321/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-321/jo-37/perl/ch-1.pl | 94 | ||||
| -rwxr-xr-x | challenge-321/jo-37/perl/ch-2.pl | 122 |
3 files changed, 217 insertions, 0 deletions
diff --git a/challenge-321/jo-37/blog.txt b/challenge-321/jo-37/blog.txt new file mode 100644 index 0000000000..5b1d900672 --- /dev/null +++ b/challenge-321/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2025/05/18/ch-321.html diff --git a/challenge-321/jo-37/perl/ch-1.pl b/challenge-321/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..a2eb886dd8 --- /dev/null +++ b/challenge-321/jo-37/perl/ch-1.pl @@ -0,0 +1,94 @@ +#!/usr/bin/perl + +use v5.26; +use Test2::V0 qw(!float -no_srand); +use Test2::Tools::Subtest 'subtest_streamed'; +use Getopt::Long; +use experimental 'signatures'; + +use PDL; +use PDL::NiceSlice; + +### Options and Arguments + +my ($tests, $examples, $verbose); +GetOptions( + 'examples!' => \$examples, + 'tests!' => \$tests, + 'verbose!' => \$verbose, +) or usage(); + +run_tests($examples, $tests); # tests do not return + +usage() unless @ARGV; + +sub usage { + die <<~EOS; + $0 - distinct averages + + usage: $0 [-examples] [-tests] [--] [N...] + + -examples + run the examples from the challenge + + -tests + run some tests + + N... + list of numbers + + EOS +} + + +### Input and Output + +say distinct_avg(@ARGV); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/05/18/ch-321.html#task-1 + +use constant SCALE => 1e8; + +sub distinct_avg { + my $n = pdl(@_)->qsort->splitdim(0, 2); + $n(1) .= $n(1,-1:0;|); + ($n->sumover * SCALE)->rint->uniq->nelem; +} + + +### Examples and Tests + +sub run_tests ($examples, $tests) { + return unless $examples || $tests; + + state sub run_example ($args, $expected, $name) { + my $result = distinct_avg(@$args); + is $result, $expected, + "$name: (@$args) -> $expected"; + } + + plan 2; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [[1, 2, 4, 3, 5, 6], 1, 'example 1'], + [[0, 2, 4, 8, 3, 5], 2, 'example 2'], + [[7, 3, 1, 0, 5, 9], 2, 'example 3'], + ); + plan scalar @examples; + for (@examples) { + run_example @$_; + } + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + plan 1; + is distinct_avg(-1.3, -.3, .7, 1.7), 1, 'floating point input'; + }) : pass 'skip tests'; + + exit; +} diff --git a/challenge-321/jo-37/perl/ch-2.pl b/challenge-321/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..b510b438e6 --- /dev/null +++ b/challenge-321/jo-37/perl/ch-2.pl @@ -0,0 +1,122 @@ +#!/usr/bin/perl + +use v5.26; +use Test2::V0 -no_srand; +use Test2::Tools::Subtest 'subtest_streamed'; +use Getopt::Long; +use experimental 'signatures'; + +use Benchmark 'cmpthese'; + + +### Options and Arguments + +my ($tests, $examples, $benchmark, $verbose); +GetOptions( + 'examples!' => \$examples, + 'tests!' => \$tests, + 'benchmark!' => \$benchmark, + 'verbose!' => \$verbose, +) or usage(); + +run_tests($examples, $tests, $benchmark); # tests do not return + +usage() unless @ARGV == 2; + +sub usage { + die <<~EOS; + $0 - backspace compare + + usage: $0 [-examples] [-tests] [-benchmark] [STR1 STR2] + + -examples + run the examples from the challenge + + -tests + run some tests + + -benchmark + run benchmark + + STR1 STR2 + two strings + + EOS +} + + +### Input and Output + +say +(qw(true false))[!bs_cmp(@ARGV)]; + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2025/05/18/ch-321.html#task-2 + +sub apply_bs { + my @out; + $_ eq '#' ? pop @out : push @out, $_ for split //, shift; + join '', @out; +} + +sub bs_cmp ($str1, $str2) { + apply_bs($str1) eq apply_bs($str2); +} + +sub bs_cmp_subst ($str1, $str2) { + for ($str1, $str2) { + 1 while s/.#//; + } + $str1 eq $str2; +} + + +### Examples and Tests + +sub run_tests ($examples, $tests, $benchmark) { + return unless $examples || $tests || $benchmark; + + state sub run_example ($args, $expected, $name) { + my $result = bs_cmp(@$args); + is $result, $expected, + "$name: (@$args) -> " . $expected->name; + } + + plan 3; + + $examples ? subtest_streamed(examples => sub { + my @examples = ( + [["ab#c", "ad#c"], T(), 'example 1'], + [["ab##", "a#b#"], T(), 'example 2'], + [["a#b", "c"], F(), 'example 3'], + ); + plan scalar @examples; + for (@examples) { + run_example @$_; + } + }) : pass 'skip examples'; + + $tests ? subtest_streamed(tests => sub { + plan 4; + ok bs_cmp("#abc", "abc"), 'leading bs'; + ok bs_cmp("a##", ""), 'bs moving to start'; + ok bs_cmp("###", ""), 'multiple leading bs'; + ok bs_cmp("abc#de###", "a"), 'delete a lot'; + }) : pass 'skip tests'; + + $benchmark ? subtest_streamed(benchmark => sub { + plan 2; + my $str1 = 'a' x 1e5 . '#' x 1e5; + my $str2 = ''; + ok bs_cmp($str1, $str2), 'check copy'; + ok bs_cmp_subst($str1, $str2), 'check subst'; + cmpthese(0, { + subst => sub {bs_cmp_subst($str1, $str2)}, + copy => sub {bs_cmp($str1, $str2)}, + }); + }) : pass 'skip benchmark'; + + exit; +} |
