aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-11-29 22:38:57 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-12-01 15:01:18 +0100
commitc89693c8ba13511dbb0d3bf822746baba001fc0f (patch)
treed8d33fb63c3007fc62145096a2b9a43ed3826df1
parent2ac2c6b618af5515c64bbdd73482636ffe2f79aa (diff)
downloadperlweeklychallenge-club-c89693c8ba13511dbb0d3bf822746baba001fc0f.tar.gz
perlweeklychallenge-club-c89693c8ba13511dbb0d3bf822746baba001fc0f.tar.bz2
perlweeklychallenge-club-c89693c8ba13511dbb0d3bf822746baba001fc0f.zip
Solution to task 1
-rwxr-xr-xchallenge-245/jo-37/perl/ch-1.pl63
1 files changed, 63 insertions, 0 deletions
diff --git a/challenge-245/jo-37/perl/ch-1.pl b/challenge-245/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..9a77fffc6d
--- /dev/null
+++ b/challenge-245/jo-37/perl/ch-1.pl
@@ -0,0 +1,63 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+use experimental 'signatures';
+
+our ($tests, $examples, $verbose);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [lang,... popularity,...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-verbose
+ enable trace output
+
+lang,...
+ list of (comma or blank) separated strings
+
+popularity,...
+ list of (comma or blank) separated numeric popularities
+
+EOS
+
+
+### Input and Output
+
+say "(@{[sort_by_popularity(map [split /[ ,]/], @ARGV)]})";
+
+
+### Implementation
+
+sub sort_by_popularity ($lang, $p8y) {
+ $lang->@[sort {$p8y->[$a] <=> $p8y->[$b]} 0 .. $p8y->$#*];
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is [sort_by_popularity(['perl', 'c', 'python'], [2, 1, 3])],
+ ['c', 'perl', 'python'], 'example 1';
+
+ is [sort_by_popularity(['c++', 'haskell', 'java'], [1, 3, 2])],
+ ['c++', 'java', 'haskell'], 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}