aboutsummaryrefslogtreecommitdiff
path: root/challenge-273
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-273')
-rw-r--r--challenge-273/jo-37/blog.txt1
-rwxr-xr-xchallenge-273/jo-37/perl/ch-1.pl68
-rwxr-xr-xchallenge-273/jo-37/perl/ch-2.pl62
3 files changed, 131 insertions, 0 deletions
diff --git a/challenge-273/jo-37/blog.txt b/challenge-273/jo-37/blog.txt
new file mode 100644
index 0000000000..a09857df8d
--- /dev/null
+++ b/challenge-273/jo-37/blog.txt
@@ -0,0 +1 @@
+https://github.sommrey.de/the-bears-den/2024/06/14/ch-273.html
diff --git a/challenge-273/jo-37/perl/ch-1.pl b/challenge-273/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..b832bb2989
--- /dev/null
+++ b/challenge-273/jo-37/perl/ch-1.pl
@@ -0,0 +1,68 @@
+#!/usr/bin/perl -s
+
+use v5.24;
+use Test2::V0;
+use experimental 'signatures';
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV == 2;
+usage: $0 [-examples] [-tests] [C STR]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+C
+ a single character (or string)
+
+STR
+ a string
+
+EOS
+
+
+### Input and Output
+
+say pct_sub(@ARGV);
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2024/06/14/ch-273.html#task-1
+
+
+sub pct_sub ($sub, $str) {
+ return int .5 + 100 * (() = /\Q$sub/g) * length($sub) / length for $str;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is pct_sub("e", "perl"), 25, 'example 1';
+ is pct_sub("a", "java"), 50, 'example 2';
+ is pct_sub("m", "python"), 0, 'example 3';
+ is pct_sub("a", "ada"), 67, 'example 4';
+ is pct_sub("l", "ballerina"), 22, 'example 5';
+ is pct_sub("k", "analitik"), 13, 'example 6';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is pct_sub("12", "123"), 67, 'not a single character';
+ is pct_sub('.$', '.$--.$'), 67, 'meta characters';
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-273/jo-37/perl/ch-2.pl b/challenge-273/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..0079a2fa55
--- /dev/null
+++ b/challenge-273/jo-37/perl/ch-2.pl
@@ -0,0 +1,62 @@
+#!/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 == 1;
+usage: $0 [-examples] [-tests] [STR]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+STR
+ a string
+
+EOS
+
+
+### Input and Output
+
+say +(qw(true false))[!b_after_a(shift)];
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2024/06/14/ch-273.html#task-2
+
+
+sub b_after_a {
+ shift =~ /^[^b]*+b[^a]*+$/;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ ok b_after_a("aabb"), 'example 1';
+ ok !b_after_a("abab"), 'example 2';
+ ok !b_after_a("aaa"), 'example 3';
+ ok b_after_a("bbb"), 'example 4';
+
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ ok b_after_a("b"), 'single b';
+ }
+
+ done_testing;
+ exit;
+}