aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xchallenge-194/jo-37/perl/ch-1.pl71
-rwxr-xr-xchallenge-194/jo-37/perl/ch-2.pl75
2 files changed, 146 insertions, 0 deletions
diff --git a/challenge-194/jo-37/perl/ch-1.pl b/challenge-194/jo-37/perl/ch-1.pl
new file mode 100755
index 0000000000..57e21ffbd0
--- /dev/null
+++ b/challenge-194/jo-37/perl/ch-1.pl
@@ -0,0 +1,71 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0;
+use experimental qw(signatures postderef);
+
+our ($tests, $examples);
+
+run_tests() if $tests || $examples; # does not return
+
+die <<EOS unless @ARGV;
+usage: $0 [-examples] [-tests] [TIME]
+
+-examples
+ run the examples from the challenge
+
+-tests
+ run some tests
+
+TIME
+ incomplete time string containing one question mark at a digit position.
+
+EOS
+
+
+### Input and Output
+
+say missing(shift);
+
+
+### Implementation
+
+sub missing ($t) {
+ # For each position of the question mark define a list of
+ # regex-value pairs that provide the maximum value that may replace
+ # the question mark if the regex matches.
+ state $match = [
+ [[qr/^.[4-9]/, 1], [qr//, 2]],
+ [[qr/^2/, 3], [qr//, 9]],
+ undef,
+ [[qr//, 5]],
+ [[qr//, 9]]];
+
+ # Apply the patterns in the list for the question mark's position
+ # and return the respective value for a match.
+ $t =~ $_->[0] && return $_->[1] for $match->[index($t, '?')]->@*;
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ is missing('?5:00'), 1, 'example 1';
+ is missing('?3:00'), 2, 'example 2';
+ is missing('1?:00'), 9, 'example 3';
+ is missing('2?:00'), 3, 'example 4';
+ is missing('12:?5'), 5, 'example 5';
+ is missing('12:5?'), 9, 'example 6';
+
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+ }
+
+ done_testing;
+ exit;
+}
diff --git a/challenge-194/jo-37/perl/ch-2.pl b/challenge-194/jo-37/perl/ch-2.pl
new file mode 100755
index 0000000000..5d85a79922
--- /dev/null
+++ b/challenge-194/jo-37/perl/ch-2.pl
@@ -0,0 +1,75 @@
+#!/usr/bin/perl -s
+
+use v5.16;
+use Test2::V0;
+use List::Util qw(pairvalues);
+use List::MoreUtils qw(frequency);
+
+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
+ a string
+
+EOS
+
+
+### Input and Output
+
+say can_equalize(shift);
+
+
+### Implementation
+
+# Putting the cart before the horse: If all characters are equally
+# distributed and we add one character:
+# - an existing frequency gets incremented or
+# - a new frequency of '1' appears.
+# Thus we can arrive at an equal distribution by removing one character
+# if:
+# - There is only one frequency.
+# - There is a single frequency of '1' and all other are equally
+# distributed.
+# - There is a single frequency with a value of one above the equally
+# distributed rest.
+# Regarding the empty string as equally distributed.
+sub can_equalize {
+ my @f = sort {$a <=> $b} pairvalues frequency split //, shift;
+ @f == 1 ||
+ $f[0] == 1 && $f[1] == $f[-1] ||
+ $f[-1] == $f[-2] + 1 && $f[0] == $f[-2];
+}
+
+
+### Examples and tests
+
+sub run_tests {
+ SKIP: {
+ skip "examples" unless $examples;
+
+ ok can_equalize('abbc'), 'example 1';
+ ok can_equalize('xyzyyxz'), 'example 2';
+ ok !can_equalize('xzxz'), 'example 3';
+ }
+
+ SKIP: {
+ skip "tests" unless $tests;
+
+ ok can_equalize('zz'), 'single frequency';
+ ok can_equalize('abbacdc'), 'singleton';
+ ok can_equalize('q'), 'equalize to empty';
+ }
+
+ done_testing;
+ exit;
+}