aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-11-20 19:34:07 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-11-24 15:40:51 +0100
commit3b5b82a83e72faf87a8ba4da7fe26ffffd813c3a (patch)
treec5ba11a239ad05320e6ceec7f6c0c7bd450abe7c
parentd931ba7fbb2d57ca1e5123ea29b80f1c0dbc28e5 (diff)
downloadperlweeklychallenge-club-3b5b82a83e72faf87a8ba4da7fe26ffffd813c3a.tar.gz
perlweeklychallenge-club-3b5b82a83e72faf87a8ba4da7fe26ffffd813c3a.tar.bz2
perlweeklychallenge-club-3b5b82a83e72faf87a8ba4da7fe26ffffd813c3a.zip
Solution to task 1
-rwxr-xr-xchallenge-244/jo-37/perl/ch-1.pl55
1 files changed, 55 insertions, 0 deletions
diff --git a/challenge-244/jo-37/perl/ch-1.pl b/challenge-244/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..cb67cc00e9
--- /dev/null
+++ b/challenge-244/jo-37/perl/ch-1.pl
@@ -0,0 +1,55 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0;
+use List::Rank 'rank';
+
+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 "(@{[count_smaller(@ARGV)]})";
+
+
+### Implementation
+
+sub count_smaller {
+ map tr/=//dr - 1, rank @_;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is [count_smaller(8, 1, 2, 2, 3)], [4, 0, 1, 1, 3], 'example 1';
+ is [count_smaller(6, 5, 4, 8)], [2, 1, 0, 3], 'example 2';
+ is [count_smaller(2, 2, 2)], [0, 0, 0], 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}