aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-02-15 21:02:19 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-02-16 15:29:45 +0100
commit359f26f7f293eb1860c52a3b6b0733b542f4ace0 (patch)
tree4467b8f8e534ddde331131a09161f802011a682d
parentc9792b8a02f80c49e9bb4f0a1056c98d41e693a4 (diff)
downloadperlweeklychallenge-club-359f26f7f293eb1860c52a3b6b0733b542f4ace0.tar.gz
perlweeklychallenge-club-359f26f7f293eb1860c52a3b6b0733b542f4ace0.tar.bz2
perlweeklychallenge-club-359f26f7f293eb1860c52a3b6b0733b542f4ace0.zip
Solution to task 1
-rwxr-xr-xchallenge-256/jo-37/perl/ch-1.pl57
1 files changed, 57 insertions, 0 deletions
diff --git a/challenge-256/jo-37/perl/ch-1.pl b/challenge-256/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..f1cc4ac2c0
--- /dev/null
+++ b/challenge-256/jo-37/perl/ch-1.pl
@@ -0,0 +1,57 @@
+#!/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;
+usage: $0 [-examples] [-tests] [W...]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+W...
+ list of words
+
+EOS
+
+
+### Input and Output
+
+say maximum_pairs(@ARGV);
+
+
+### Implementation
+
+sub maximum_pairs {
+ (\my %words)->@{@_} = map scalar reverse, @_;
+
+ delete @words{grep $_ eq $words{$_}, keys %words};
+ (grep defined, delete @words{values %words}) / 2;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is maximum_pairs("ab", "de", "ed", "bc"), 1, 'example 1';
+ is maximum_pairs("aa", "ba", "cd", "ed"), 0, 'example 2';
+ is maximum_pairs("uv", "qp", "st", "vu", "mn", "pq"), 2, 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}