aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-05-31 10:38:11 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2023-06-02 14:53:42 +0200
commit88fb71ed321efb0ed032accaa14e09e51509e0fe (patch)
treead7151ab737a9acf93656be556c82f1a4e0ee07c
parent7990d15d06fe7edccb9b81de5b3614422bcc3ee4 (diff)
downloadperlweeklychallenge-club-88fb71ed321efb0ed032accaa14e09e51509e0fe.tar.gz
perlweeklychallenge-club-88fb71ed321efb0ed032accaa14e09e51509e0fe.tar.bz2
perlweeklychallenge-club-88fb71ed321efb0ed032accaa14e09e51509e0fe.zip
Solution to task 1
-rwxr-xr-xchallenge-219/jo-37/perl/ch-1.pl56
1 files changed, 56 insertions, 0 deletions
diff --git a/challenge-219/jo-37/perl/ch-1.pl b/challenge-219/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..7f0fd3232d
--- /dev/null
+++ b/challenge-219/jo-37/perl/ch-1.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+
+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 "@{[sorted_squares(@ARGV)]}";
+
+
+### Implementation
+
+sub sorted_squares {
+ sort {$a <=> $b} map $_ * $_, @_;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is [sorted_squares(-2, -1, 0, 3, 4)],
+ [0, 1, 4, 9, 16], 'example 1';
+
+ is [sorted_squares(5, -4, -1, 3, 6)],
+ [1, 9, 16, 25, 36], 'example 2';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}