diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-05-25 17:06:58 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-25 17:06:58 +0100 |
| commit | f1801cb3652552736263adc634b168ddeb0ec574 (patch) | |
| tree | 6f29644ae5a687e20715be84bc135ab9de1f0c0e | |
| parent | fca7eb2dda0c866c74a68e38be940b653ed1918f (diff) | |
| parent | db987c3c078a8b1278a239d6644403d097d78c0c (diff) | |
| download | perlweeklychallenge-club-f1801cb3652552736263adc634b168ddeb0ec574.tar.gz perlweeklychallenge-club-f1801cb3652552736263adc634b168ddeb0ec574.tar.bz2 perlweeklychallenge-club-f1801cb3652552736263adc634b168ddeb0ec574.zip | |
Merge pull request #10142 from jo-37/contrib
Solutions to challenge 270
| -rw-r--r-- | challenge-270/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-270/jo-37/perl/ch-1.pl | 67 | ||||
| -rwxr-xr-x | challenge-270/jo-37/perl/ch-2.pl | 78 |
3 files changed, 146 insertions, 0 deletions
diff --git a/challenge-270/jo-37/blog.txt b/challenge-270/jo-37/blog.txt new file mode 100644 index 0000000000..fc81d9be72 --- /dev/null +++ b/challenge-270/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2024/05/24/ch-270.html diff --git a/challenge-270/jo-37/perl/ch-1.pl b/challenge-270/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..932b41e780 --- /dev/null +++ b/challenge-270/jo-37/perl/ch-1.pl @@ -0,0 +1,67 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0 '!float'; +use PDL; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [M] + +-examples + run the examples from the challenge + +-tests + run some tests + +M + matrix in any form acceptedd by the PDL string constructor, e.g. + '[ [1, 0, 0], [0, 0, 1], [1, 0, 0] ]' + + +EOS + + +### Input and Output + +say count_special("@ARGV"); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2024/05/24/ch-270.html#task-1 + + +sub count_special { + my $m = pdl @_; + which($m * $m->sumover->dummy(0) * $m->xchg(0, 1)->sumover == 1)->nelem; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is count_special([ + [1, 0, 0], + [0, 0, 1], + [1, 0, 0],]), 1, 'example 1'; + is count_special([ + [1, 0, 0], + [0, 1, 0], + [0, 0, 1],]), 3, 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} diff --git a/challenge-270/jo-37/perl/ch-2.pl b/challenge-270/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..05435dc0ed --- /dev/null +++ b/challenge-270/jo-37/perl/ch-2.pl @@ -0,0 +1,78 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0 '!float'; +use PDL v2.077; # for minimum_n_ind +use PDL::NiceSlice; +use experimental 'signatures'; + +our ($tests, $examples, $verbose, $x, $y); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless defined $x && defined $y && @ARGV; +usage: $0 [-examples] [-tests] [-verbose] [-x=X -y=Y INT...] + +-examples + run the examples from the challenge + +-tests + run some tests + +-verbose + show intermediate steps + +-x=X + cost for 'level 1' operations + +-y=Y + cost for 'level 2' operations + +INT... + list of integers +EOS + + +### Input and Output + +say equalize_array($x, $y, @ARGV); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2024/05/24/ch-270.html#task-2 + + +sub equalize_array ($x, $y, @ints) { + my $ints = pdl @ints; + return 0 if $ints->dim(0) < 2; + my $target = maximum $ints; + return $x * ($ints->dim(0) * $target - sum($ints)) if $y >= 2 * $x; + my $cost = 0; + while () { + say $ints if $verbose; + my $min = $ints->index(minimum_n_ind($ints, 2)); + return $cost + $x * ($target - $min((0))) if $min((1)) == $target; + $min += 1; + $cost += $y; + } +} + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is equalize_array(3, 2 ,=> 4, 1), 9, 'example 1'; + is equalize_array(2, 1 ,=> 2, 3, 3, 3, 5), 6, 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} |
