From b4c1e38aabf292a92866dac8d19baca16d9abc5f Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Sun, 18 May 2025 12:18:12 +0200 Subject: Solution to task 1 --- challenge-321/jo-37/perl/ch-1.pl | 94 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100755 challenge-321/jo-37/perl/ch-1.pl 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; +} -- cgit From 87773501bebca3bae9a15dc8bd3e7e8da824cc10 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Sun, 18 May 2025 12:18:37 +0200 Subject: Solution to task 2 --- challenge-321/jo-37/perl/ch-2.pl | 122 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100755 challenge-321/jo-37/perl/ch-2.pl 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; +} -- cgit From 4ed2e8598bd423d511013f638d84df8daedc61f1 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Sun, 18 May 2025 12:18:55 +0200 Subject: Blog for challenge 321 --- challenge-321/jo-37/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-321/jo-37/blog.txt 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 -- cgit