aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-09-13 14:38:24 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-09-13 14:38:24 +0200
commit255edec3dcf61efc919de7a7bcd5ef9af6769c2d (patch)
tree8a32f2b4cc785178cb73227635164fd5e04628b2
parent92704d24e719f7514dcfc74a71fa03f1ea8f8f06 (diff)
parente5648bcb029740eb843e750a5f7cd1f1d896d435 (diff)
downloadperlweeklychallenge-club-255edec3dcf61efc919de7a7bcd5ef9af6769c2d.tar.gz
perlweeklychallenge-club-255edec3dcf61efc919de7a7bcd5ef9af6769c2d.tar.bz2
perlweeklychallenge-club-255edec3dcf61efc919de7a7bcd5ef9af6769c2d.zip
Solutions to challenge 286
-rw-r--r--challenge-286/jo-37/blog.txt1
-rwxr-xr-xchallenge-286/jo-37/perl/ch-1.pl7
-rwxr-xr-xchallenge-286/jo-37/perl/ch-1a.sh7
-rwxr-xr-xchallenge-286/jo-37/perl/ch-2.pl73
4 files changed, 88 insertions, 0 deletions
diff --git a/challenge-286/jo-37/blog.txt b/challenge-286/jo-37/blog.txt
new file mode 100644
index 0000000000..e22b2684cd
--- /dev/null
+++ b/challenge-286/jo-37/blog.txt
@@ -0,0 +1 @@
+https://github.sommrey.de/the-bears-den/2024/09/13/ch-286.html
diff --git a/challenge-286/jo-37/perl/ch-1.pl b/challenge-286/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..ad5d8fd247
--- /dev/null
+++ b/challenge-286/jo-37/perl/ch-1.pl
@@ -0,0 +1,7 @@
+#!/usr/bin/perl -w0777
+
+use v5.12;
+seek DATA, 0, 0;
+say +(keys %{ {<DATA> =~ /(\S+)()/g} })[0];
+__DATA__
+Hello David!
diff --git a/challenge-286/jo-37/perl/ch-1a.sh b/challenge-286/jo-37/perl/ch-1a.sh
new file mode 100755
index 0000000000..9386ee22f8
--- /dev/null
+++ b/challenge-286/jo-37/perl/ch-1a.sh
@@ -0,0 +1,7 @@
+#!/bin/bash
+
+# usage: ch-1.sh [N]
+# call ch-1.pl N times (default: 80), count the individual printed words
+# and sort by frequency
+
+for i in $(seq 1 ${1:-80}); do ./ch-1.pl; done | sort | uniq -c | sort -n
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;
+}