From ad04e23250080bfc5e03f8159fd8ff3e5a5bc4e7 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Wed, 21 Feb 2024 22:13:32 +0100 Subject: Solution to task 1 --- challenge-257/jo-37/perl/ch-1.pl | 81 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100755 challenge-257/jo-37/perl/ch-1.pl diff --git a/challenge-257/jo-37/perl/ch-1.pl b/challenge-257/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..93bd7fb00c --- /dev/null +++ b/challenge-257/jo-37/perl/ch-1.pl @@ -0,0 +1,81 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0 '!float'; +use PDL; +use PDL::NiceSlice; +use Benchmark 'cmpthese'; + +our ($tests, $examples, $benchmark); + +run_tests() if $tests || $examples; # does not return + +die <qsorti; + cat($si, sequence($l) - $l->($si)->dummy(0)->enumvec) + ->xchg(0,1)->qsortvec->((1)); +} + +sub sdc_full { + my $l = long @_; + ($l < $l->dummy(0))->sumover; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is sdc(5, 2, 1, 6)->unpdl, [2, 1, 0, 3], 'example 1'; + is sdc(1, 2, 0, 3)->unpdl, [1, 2, 0, 3], 'example 2'; + is sdc(0, 1)->unpdl, [0, 1], 'example 3'; + is sdc(9, 4, 9, 2)->unpdl, [2, 1, 2, 0], 'example 4'; + + } + + SKIP: { + skip "tests" unless $tests; + } + + SKIP: { + skip "benchmark" unless $benchmark; + + my $n = 10000; + my $l = ($n * random $n)->long; + ok all(sdc_full($l) == sdc($l)), 'cross-check'; + + cmpthese(0, { + full => sub {sdc_full($l)}, + rank => sub {sdc($l)} + }); + + } + + done_testing; + exit; +} -- cgit From f1ee236233d1f06344b247eef2eeabd5381a7220 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Wed, 21 Feb 2024 22:10:27 +0100 Subject: Solution to task 2 --- challenge-257/jo-37/perl/ch-2.pl | 119 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100755 challenge-257/jo-37/perl/ch-2.pl diff --git a/challenge-257/jo-37/perl/ch-2.pl b/challenge-257/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..89027b5768 --- /dev/null +++ b/challenge-257/jo-37/perl/ch-2.pl @@ -0,0 +1,119 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0 '!float'; +use PDL; +use PDL::NiceSlice; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <(1)->enumvec == 0); + my $firstones = $allones->dice('X', $firstidx); + return unless $firstones->dim(1) < 2 || + all $firstones((0),0:-2) < $firstones((0),1:-1); + for my $firstone ($firstones->dog) { + my ($col, $row) = $firstone->list; + return unless 1 == sum $m->dice('X', $row)->(0:$col) != 0; + return unless 1 == sum $m->dice($col, 'X')->(,0:$row) != 0; + } + my $zeros = which $m->orover == 0; + return unless !$firstones->ngood || !$zeros->ngood || + $firstones->((1))->max < $zeros->min; + + return unless $zeros->dim(0) + $firstidx->dim(0) == $m->dim(1); + + 1; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + ok !is_reduced_echelon([ + [1, 1, 0], + [0, 1, 0], + [0, 0, 0] + ]), 'example 1'; + + ok is_reduced_echelon([ + [0, 1,-2, 0, 1], + [0, 0, 0, 1, 3], + [0, 0, 0, 0, 0], + [0, 0, 0, 0, 0] + ]), 'example 2'; + + ok is_reduced_echelon([ + [1, 0, 0, 4], + [0, 1, 0, 7], + [0, 0, 1,-1] + ]), 'example 3'; + + ok !is_reduced_echelon([ + [0, 1,-2, 0, 1], + [0, 0, 0, 0, 0], + [0, 0, 0, 1, 3], + [0, 0, 0, 0, 0] + ]), 'example 4'; + + ok !is_reduced_echelon([ + [0, 1, 0], + [1, 0, 0], + [0, 0, 0] + ]), 'example 5'; + + ok !is_reduced_echelon([ + [4, 0, 0, 0], + [0, 1, 0, 7], + [0, 0, 1,-1] + ]), 'example 6'; + } + + SKIP: { + skip "tests" unless $tests; + + ok is_reduced_echelon([ + [0, 0, 0], + [0, 0, 0] + ]), 'zero matrix'; + + ok is_reduced_echelon([ + [0, 0, 1], + [0, 0, 0] + ]), 'single row'; + } + + done_testing; + exit; +} -- cgit From c610b2dd201719b64017cea47694cb745751cf90 Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Fri, 23 Feb 2024 21:48:46 +0100 Subject: Blog for challenge 257 --- challenge-257/jo-37/blog.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 challenge-257/jo-37/blog.txt diff --git a/challenge-257/jo-37/blog.txt b/challenge-257/jo-37/blog.txt new file mode 100644 index 0000000000..8354d02fdc --- /dev/null +++ b/challenge-257/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2024/02/23/ch-257.html -- cgit