diff options
| author | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-08-30 13:31:07 +0200 |
|---|---|---|
| committer | Jörg Sommrey <28217714+jo-37@users.noreply.github.com> | 2024-08-30 17:09:02 +0200 |
| commit | a51f5637cacfedcc699c6783f6d0a7de8c0f2c2e (patch) | |
| tree | 9cd6563aabf9ba843ee731087bc830da188738d3 | |
| parent | 9d8b35adb9460155304b6fbaf2b2480c6c7fe723 (diff) | |
| download | perlweeklychallenge-club-a51f5637cacfedcc699c6783f6d0a7de8c0f2c2e.tar.gz perlweeklychallenge-club-a51f5637cacfedcc699c6783f6d0a7de8c0f2c2e.tar.bz2 perlweeklychallenge-club-a51f5637cacfedcc699c6783f6d0a7de8c0f2c2e.zip | |
Solution to task 1
| -rwxr-xr-x | challenge-284/jo-37/perl/ch-1.pl | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/challenge-284/jo-37/perl/ch-1.pl b/challenge-284/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..ceab3d9239 --- /dev/null +++ b/challenge-284/jo-37/perl/ch-1.pl @@ -0,0 +1,66 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0 '!float'; +use PDL '2.017'; +use PDL::NiceSlice; + +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 integers + +EOS + + +### Input and Output + +say lucky_integer(@ARGV); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2024/08/30/ch-284.html#task-1 + + +sub lucky_integer { + my ($freq, $val) = rle long(@_)->qsort; + my $lucky = $val->where($freq == $val); + $lucky->isempty ? -1 : $lucky(-1;-); +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is lucky_integer(2, 2, 3, 4), 2, 'example 1'; + is lucky_integer(1, 2, 2, 3, 3, 3), 3, 'example 2'; + is lucky_integer(1, 1, 1, 3), -1, 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + + is lucky_integer( + qw(11 1 11 2 11 2 11 3 11 3 11 3 11 4 11 4 11 4 11 4 11)), 11, + 'these go to eleven'; + } + + done_testing; + exit; +} |
