aboutsummaryrefslogtreecommitdiff
path: root/challenge-307
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2025-02-07 11:00:12 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2025-02-07 11:06:54 +0100
commit07ce8b170ad1b8e91d653c9d7dd64c7f3dc210d2 (patch)
tree0c5042c2b6bca0e65207271a6ac16a3fcb5d38b5 /challenge-307
parentea528b3a4f9f371f274a0c9c3012f49cb5e2bed0 (diff)
downloadperlweeklychallenge-club-07ce8b170ad1b8e91d653c9d7dd64c7f3dc210d2.tar.gz
perlweeklychallenge-club-07ce8b170ad1b8e91d653c9d7dd64c7f3dc210d2.tar.bz2
perlweeklychallenge-club-07ce8b170ad1b8e91d653c9d7dd64c7f3dc210d2.zip
Solution to task 1
Diffstat (limited to 'challenge-307')
-rwxr-xr-xchallenge-307/jo-37/perl/ch-1.pl93
1 files changed, 93 insertions, 0 deletions
diff --git a/challenge-307/jo-37/perl/ch-1.pl b/challenge-307/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..744d9dced3
--- /dev/null
+++ b/challenge-307/jo-37/perl/ch-1.pl
@@ -0,0 +1,93 @@
+#!/usr/bin/perl
+
+use v5.26;
+use Test2::V0 '!float';
+use Test2::Tools::Subtest 'subtest_streamed';
+use Getopt::Long;
+use experimental 'signatures';
+
+use PDL;
+
+### Options and Arguments
+
+my ($tests, $examples, $verbose);
+GetOptions(
+ 'examples!' => \$examples,
+ 'tests!' => \$tests,
+ 'verbose!' => \$verbose,
+) or usage();
+
+run_tests($examples, $tests); # tests do not return
+
+usage() unless @ARGV;
+
+sub usage {
+ die <<~EOS;
+ $0 - Check order
+
+ 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 "(@{check_order(@ARGV)->unpdl})";
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2025/02/07/ch-307.html#task-1
+
+
+sub check_order {
+ my $ints = long \@_;
+ which $ints != $ints->qsort;
+}
+
+
+### Examples and Tests
+
+sub run_tests ($examples, $tests) {
+ return unless $examples || $tests;
+
+ state sub run_example ($args, $expected, $name) {
+ my $result = check_order(@$args)->unpdl;
+ is $result, $expected,
+ "$name: (@$args) -> (@$expected)";
+ }
+
+ plan 2;
+
+ $examples ? subtest_streamed(examples => sub {
+ my @examples = (
+ [[5, 2, 4, 3, 1], [0, 2, 3, 4], 'example 1'],
+ [[1, 2, 1, 1, 3], [1, 3], 'example 2'],
+ [[3, 1, 3, 2, 3], [0, 1, 3], 'example 3'],
+ );
+ plan scalar @examples;
+ for (@examples) {
+ run_example @$_;
+ }
+ }) : pass 'skip examples';
+
+ $tests ? subtest_streamed(tests => sub {
+ plan 3;
+ is check_order(2, 3, 5, 7, 11)->unpdl, [], 'sorted';
+ is check_order()->unpdl, [], 'empty';
+ is check_order(42)->unpdl, [], 'singleton';
+ }) : pass 'skip tests';
+
+ exit;
+}