aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-02-06 18:04:35 +0100
committerJörg Sommrey <28217714+jo-37@users.noreply.github.com>2024-02-09 18:11:08 +0100
commit1fe5253aebbcb4e8c92635f9c6fbb7218b439ac6 (patch)
tree9b0090498639f1656de4c6f8c64094b4ad2b196b
parent399287fcd1fa2800c9ab44b9a065116e9a55ec86 (diff)
downloadperlweeklychallenge-club-1fe5253aebbcb4e8c92635f9c6fbb7218b439ac6.tar.gz
perlweeklychallenge-club-1fe5253aebbcb4e8c92635f9c6fbb7218b439ac6.tar.bz2
perlweeklychallenge-club-1fe5253aebbcb4e8c92635f9c6fbb7218b439ac6.zip
Solution to task 1
-rwxr-xr-xchallenge-255/jo-37/perl/ch-1.pl61
1 files changed, 61 insertions, 0 deletions
diff --git a/challenge-255/jo-37/perl/ch-1.pl b/challenge-255/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..1ecc01addb
--- /dev/null
+++ b/challenge-255/jo-37/perl/ch-1.pl
@@ -0,0 +1,61 @@
+#!/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] [S T]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+S T
+ two strings
+
+EOS
+
+
+### Input and Output
+
+say "@{[char_diff(@ARGV)]}";
+
+
+### Implementation
+
+sub char_diff ($s, $t) {
+ my %count;
+ $count{$_}++ for split //, $t;
+ $count{$_}-- for split //, $s;
+ no warnings 'numeric';
+ map +($_) x $count{$_}, keys %count;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is [char_diff('Perl', 'Preel')], ['e'], 'example 1';
+ is [char_diff('Weekly', 'Weeakly')], ['a'], 'example 2';
+ is [char_diff('Box', 'Boxy')], ['y'], 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ is [char_diff('abcde', 'ddd')], ['d', 'd'], 'more than one';
+ }
+
+ done_testing;
+ exit;
+}