aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-08-02 21:38:18 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-08-02 21:38:18 +0200
commitdb9338fc6ae939315564be0822ac446f45471124 (patch)
treef7a485f48024e294cf60691932c5048fbf2f972e
parent107a44513e966e7cd37501766c573e3c97688680 (diff)
downloadperlweeklychallenge-club-db9338fc6ae939315564be0822ac446f45471124.tar.gz
perlweeklychallenge-club-db9338fc6ae939315564be0822ac446f45471124.tar.bz2
perlweeklychallenge-club-db9338fc6ae939315564be0822ac446f45471124.zip
Solution to task 1
-rwxr-xr-xchallenge-228/jo-37/perl/ch-1.pl56
1 files changed, 56 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;
+}