diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-08-06 19:59:27 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-08-06 19:59:27 +0100 |
| commit | 9027feaa28d00417544f0d8940caf01e095bc51f (patch) | |
| tree | 8d62e858cd2a075871b7cc14c9b3e0cb42104f1a | |
| parent | 10ec616c81e28fa2bdd7230718941fc16f28d49a (diff) | |
| parent | 93bd6df4463f54e0c454ce5af9dd3870ef03fb04 (diff) | |
| download | perlweeklychallenge-club-9027feaa28d00417544f0d8940caf01e095bc51f.tar.gz perlweeklychallenge-club-9027feaa28d00417544f0d8940caf01e095bc51f.tar.bz2 perlweeklychallenge-club-9027feaa28d00417544f0d8940caf01e095bc51f.zip | |
Merge pull request #8499 from jo-37/contrib
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; +} |
