diff options
| -rw-r--r-- | challenge-276/jo-37/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-276/jo-37/perl/ch-1.pl | 65 | ||||
| -rwxr-xr-x | challenge-276/jo-37/perl/ch-2.pl | 68 |
3 files changed, 134 insertions, 0 deletions
diff --git a/challenge-276/jo-37/blog.txt b/challenge-276/jo-37/blog.txt new file mode 100644 index 0000000000..4d33530d05 --- /dev/null +++ b/challenge-276/jo-37/blog.txt @@ -0,0 +1 @@ +https://github.sommrey.de/the-bears-den/2024/07/05/ch-276.html diff --git a/challenge-276/jo-37/perl/ch-1.pl b/challenge-276/jo-37/perl/ch-1.pl new file mode 100755 index 0000000000..0883bc8b11 --- /dev/null +++ b/challenge-276/jo-37/perl/ch-1.pl @@ -0,0 +1,65 @@ +#!/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] [H...] + +-examples + run the examples from the challenge + +-tests + run some tests + +H... + list of integers + +EOS + + +### Input and Output + +say complete_days(@ARGV); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2024/07/05/ch-276.html#task-1 + + +sub complete_days { + my $c = hist pdl(@_) % 24, -0.5, 23.5, 1; + + sum $c * ($c - 1) / 2; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is complete_days(12, 12, 30, 24, 24), 2, 'example 1'; + is complete_days(72, 48, 24, 5), 3, 'example 2'; + is complete_days(12, 18, 24), 0, 'example 3'; + } + + SKIP: { + skip "tests" unless $tests; + + is complete_days(0, 1, 2, 12, 24, 25, 26, 37, 49, 50, 74, 98), + 14, 'pairs from parts of 2, 3 and 5 elements'; + + } + + done_testing; + exit; +} diff --git a/challenge-276/jo-37/perl/ch-2.pl b/challenge-276/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..40c1c23c22 --- /dev/null +++ b/challenge-276/jo-37/perl/ch-2.pl @@ -0,0 +1,68 @@ +#!/usr/bin/perl -s + +use v5.24; +use Test2::V0; +use List::Util 'reduce'; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die <<EOS unless @ARGV; +usage: $0 [-examples] [-tests] [I...] + +-examples + run the examples from the challenge + +-tests + run some tests + +I... + list of items + +EOS + + +### Input and Output + +say max_freq(@ARGV); + + +### Implementation +# +# For details see: +# https://github.sommrey.de/the-bears-den/2024/07/05/ch-276.html#task-2 + + +sub max_freq { + my ($maxfreq, %freq) = 0; + + reduce { + &{ ( sub {$a + $maxfreq}, + sub {$maxfreq = $freq{$b}}, + sub {$a} + )[++$freq{$b} <=> $maxfreq] + }() + } 0, @_; +} + + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is max_freq(1, 2, 2, 4, 1, 5), 4, 'example 1'; + is max_freq(1, 2, 3, 4, 5), 5, 'example 2'; + } + + SKIP: { + skip "tests" unless $tests; + + is max_freq(qw(a b a b c d c d e f e f e c a)), 9, 'non-numbers' + } + + done_testing; + exit; +} |
