aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-06-28 16:20:26 +0200
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-06-28 16:20:26 +0200
commit78d9e515904db437637afb450d49a322652e7acd (patch)
tree52ea99c33504041f09d0a6b2b8d91bc5b46aed32
parent9c6ef8e490db274e7f377159bd62d9eb7a1a3042 (diff)
parentb94498b2322f611dcfb23fb60ff8152a986ae545 (diff)
downloadperlweeklychallenge-club-78d9e515904db437637afb450d49a322652e7acd.tar.gz
perlweeklychallenge-club-78d9e515904db437637afb450d49a322652e7acd.tar.bz2
perlweeklychallenge-club-78d9e515904db437637afb450d49a322652e7acd.zip
Solutions to challenge 275
-rw-r--r--challenge-275/jo-37/blog.txt1
-rwxr-xr-xchallenge-275/jo-37/perl/ch-1.pl70
-rwxr-xr-xchallenge-275/jo-37/perl/ch-2.pl78
3 files changed, 149 insertions, 0 deletions
diff --git a/challenge-275/jo-37/blog.txt b/challenge-275/jo-37/blog.txt
new file mode 100644
index 0000000000..f4a376839a
--- /dev/null
+++ b/challenge-275/jo-37/blog.txt
@@ -0,0 +1 @@
+https://github.sommrey.de/the-bears-den/2024/06/28/ch-275.html
diff --git a/challenge-275/jo-37/perl/ch-1.pl b/challenge-275/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..bc127d9e4d
--- /dev/null
+++ b/challenge-275/jo-37/perl/ch-1.pl
@@ -0,0 +1,70 @@
+#!/usr/bin/perl -s
+
+use v5.26;
+use Test2::V0;
+
+our ($tests, $examples, $verbose, $broken);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless $broken =~ /\w+/ && @ARGV;
+usage: $0 [-examples] [-tests] [-verbose] [-broken=BROKEN] [WORD...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+-verbose
+ print typeable words instead of counting them
+
+-broken=BROKEN
+ string of broken keys
+
+WORD...
+ list of words
+
+EOS
+
+
+### Input and Output
+
+main: {
+ my @typeable = broken_keys([split //, $broken], "@ARGV");
+ say $verbose ? "@typeable" : scalar @typeable;
+}
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2024/06/28/ch-275.html#task-1
+
+
+sub broken_keys {
+ my $typeable = qr{^[^@{+shift}]+$}ixx;
+
+ grep /$typeable/, split /\s+/, shift;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is scalar broken_keys(['l', 'a'], 'Perl Weekly Challenge'), 0, 'example 1';
+ is scalar broken_keys(['a'], 'Perl and Raku'), 1, 'example 2';
+ is scalar broken_keys(['l', 'o'], 'Well done Team PWC'), 2, 'example 3';
+ is scalar broken_keys(['T'], 'The joys of polyglottism'), 2, 'example 4';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-275/jo-37/perl/ch-2.pl b/challenge-275/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..49674325c8
--- /dev/null
+++ b/challenge-275/jo-37/perl/ch-2.pl
@@ -0,0 +1,78 @@
+#!/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] [AN]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+AN
+ alpha-numeric string
+
+EOS
+
+
+### Input and Output
+
+say replace_digits(shift);
+
+
+### Implementation
+#
+# For details see:
+# https://github.sommrey.de/the-bears-den/2024/06/28/ch-275.html#task-2
+
+{
+ my (@d2a, %a2i);
+ BEGIN {
+ @d2a = ('A' .. 'Z', 'a' .. 'z');
+ @a2i{@d2a} = (0 .. $#d2a);
+ }
+
+ sub replace_digits {
+ my $prev = 0;
+
+ join '', map /\d/ ? $d2a[($prev + $_) % @d2a] :
+ do {$prev = $a2i{$_} // die "Illegal char '$_'"; $_},
+ split //, shift;
+ }
+
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is replace_digits('a1c1e1'), 'abcdef', 'example 1';
+ is replace_digits('a1b2c3d4'), 'abbdcfdh', 'example 2';
+ is replace_digits('b2b'), 'bdb', 'example 3';
+ is replace_digits('a16z'), 'abgz', 'example 4';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is replace_digits('a1C1e1'), 'abCDef', 'upper case';
+ is replace_digits('Z1'), 'Za', 'wrap upper to lower';
+ is replace_digits('z1'), 'zA', 'wrap lower to upper';
+ is replace_digits('1C1'), 'BCD', 'leading digit';
+ is replace_digits('a0'), 'aa', 'zero';
+ like dies {replace_digits('a0+')}, qr/Illegal char/, 'illegal char';
+ }
+
+ done_testing;
+ exit;
+}