aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-09-13 11:29:38 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-09-13 14:37:57 +0200
commit15c11d149b4513fcdaad12e0ae08a75e05c81d0a (patch)
tree5ac36ae603b8113562138bcd59d07138c1b12845
parent517cccfd3ccf75aab663b3ce820eae0479ed0400 (diff)
downloadperlweeklychallenge-club-15c11d149b4513fcdaad12e0ae08a75e05c81d0a.tar.gz
perlweeklychallenge-club-15c11d149b4513fcdaad12e0ae08a75e05c81d0a.tar.bz2
perlweeklychallenge-club-15c11d149b4513fcdaad12e0ae08a75e05c81d0a.zip
Solution to task 2
-rwxr-xr-xchallenge-286/jo-37/perl/ch-2.pl73
1 files changed, 73 insertions, 0 deletions
diff --git a/challenge-286/jo-37/perl/ch-2.pl b/challenge-286/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..be2640fab7
--- /dev/null
+++ b/challenge-286/jo-37/perl/ch-2.pl
@@ -0,0 +1,73 @@
+#!/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] [-verbose] [--] [I...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-verbose
+ print intermediate results
+
+I...
+ list of integers
+
+EOS
+
+
+### Input and Output
+
+say order_game(@ARGV);
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2024/09/13/ch-286.html#task-2
+
+
+sub order_game (@ints) {
+ my $f = 1;
+ say "@ints" if $verbose;
+ while (@ints > 1) {
+ my ($i, $k) = splice @ints, 0, 2;
+ push @ints, ($f *= -1) * ($k - $i) > 0 ? $k : $i;
+ say "@ints" if $verbose;
+ }
+ shift @ints;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is order_game(2, 1, 4, 5, 6, 3, 0, 2), 1, 'example 1';
+ is order_game(0, 5, 3, 2), 0, 'example 2';
+ is order_game(9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8),
+ 2, 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is order_game(3, 2, 1), 2, 'not a power of two';
+
+ }
+
+ done_testing;
+ exit;
+}