diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-08-04 15:51:34 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2023-08-04 15:51:34 +0200 |
| commit | 93bd6df4463f54e0c454ce5af9dd3870ef03fb04 (patch) | |
| tree | 8d62e858cd2a075871b7cc14c9b3e0cb42104f1a | |
| parent | 10ec616c81e28fa2bdd7230718941fc16f28d49a (diff) | |
| parent | fe58c36392d0ccbda93b896a2625c658ba3b7470 (diff) | |
| download | perlweeklychallenge-club-93bd6df4463f54e0c454ce5af9dd3870ef03fb04.tar.gz perlweeklychallenge-club-93bd6df4463f54e0c454ce5af9dd3870ef03fb04.tar.bz2 perlweeklychallenge-club-93bd6df4463f54e0c454ce5af9dd3870ef03fb04.zip | |
Solutions to challenge 228
| -rwxr-xr-x | challenge-228/jo-37/perl/ch-1.pl | 56 | ||||
| -rwxr-xr-x | challenge-228/jo-37/perl/ch-2.pl | 59 |
2 files changed, 115 insertions, 0 deletions
diff --git a/challenge-228/jo-37/perl/ch-1.pl b/challenge-228/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..d9a0e02b7c --- /dev/null +++ b/challenge-228/jo-37/perl/ch-1.pl @@ -0,0 +1,56 @@ +#!/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] [--] [N...] + +-examples + run the examples from the challenge + +-tests + run some tests + +N... + list of numbers + +EOS + + +### Input and Output + +say uniq_sum(@ARGV); + + +### Implementation + +sub uniq_sum { + my ($freq, $val) = long(\@_)->qsort->rle; + $val->where($freq == 1)->sum; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is uniq_sum(2, 1, 3, 2), 4, 'example 1'; + is uniq_sum(1, 1, 1, 1), 0, 'example 2'; + is uniq_sum(2, 1, 3, 4), 10, 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} diff --git a/challenge-228/jo-37/perl/ch-2.pl b/challenge-228/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..ce0d921515 --- /dev/null +++ b/challenge-228/jo-37/perl/ch-2.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +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 zero_array(@ARGV); + + +### Implementation + +sub zero_array { + my @sort = sort {$a <=> $b} @_; + my ($head, $count); + while (@_) { + ($head = shift) == $sort[0] ? shift @sort : push @_, $head; + $count++; + } + $count; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is zero_array(3, 4, 2), 5, 'example 1'; + is zero_array(1, 2, 3), 3, 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + } + + done_testing; + exit; +} |
