aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-272/jo-37/blog.txt1
-rwxr-xr-xchallenge-272/jo-37/perl/ch-1.pl65
-rwxr-xr-xchallenge-272/jo-37/perl/ch-2.pl65
3 files changed, 131 insertions, 0 deletions
diff --git a/challenge-272/jo-37/blog.txt b/challenge-272/jo-37/blog.txt
new file mode 100644
index 0000000000..39fed0c9b1
--- /dev/null
+++ b/challenge-272/jo-37/blog.txt
@@ -0,0 +1 @@
+https://github.sommrey.de/the-bears-den/2024/06/07/ch-272.html
diff --git a/challenge-272/jo-37/perl/ch-1.pl b/challenge-272/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..9ec3849c1b
--- /dev/null
+++ b/challenge-272/jo-37/perl/ch-1.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+use Regexp::Common 'net';
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [STR]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+STR
+ "defang" string
+
+EOS
+
+
+### Input and Output
+
+say defang(shift);
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2024/06/07/ch-272.html#task-1
+
+
+sub defang {
+ return /^$RE{net}{IPv4}$/ &&
+
+ s([.])([.])gr
+
+ for shift;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is defang("1.1.1.1"), "1[.]1[.]1[.]1", "example 1";
+ is defang("255.101.1.0"), "255[.]101[.]1[.]0", "example 2";
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is defang("256.0.0.1"), F(), 'invalid address';
+ is defang("1.0.0.256"), F(), 'invalid address';
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-272/jo-37/perl/ch-2.pl b/challenge-272/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..7f1ee3903f
--- /dev/null
+++ b/challenge-272/jo-37/perl/ch-2.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0 '!float';
+use PDL;
+use PDL::NiceSlice;
+use PDL::Char;
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [STR]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-verbose
+ enable trace output
+
+STR
+ an ASCII string
+
+EOS
+
+
+### Input and Output
+
+say score(shift);
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2024/06/07/ch-272.html#task-2
+
+
+sub score {
+ my $s = PDL::Char->new(shift);
+ sum abs long($s(0:-2)) - long($s(1:-1));
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is score("hello"), 13, "example 1";
+ is score("perl"), 30, "example 2";
+ is score("raku"), 37, "example 3";
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}