aboutsummaryrefslogtreecommitdiff
path: root/challenge-194
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-12-09 11:16:21 +0000
committerGitHub <noreply@github.com>2022-12-09 11:16:21 +0000
commit7797197a1f36db05f469f41d96bf11856c0b3a14 (patch)
tree38813aa88a8d3d4dfbcb2d9655f5cac84c0cafe3 /challenge-194
parent3147357a01f64e106efc6e343b3908ad53a0ea19 (diff)
parent05d7cebd511c1c2711c2aaac0f21413172912c4a (diff)
downloadperlweeklychallenge-club-7797197a1f36db05f469f41d96bf11856c0b3a14.tar.gz
perlweeklychallenge-club-7797197a1f36db05f469f41d96bf11856c0b3a14.tar.bz2
perlweeklychallenge-club-7797197a1f36db05f469f41d96bf11856c0b3a14.zip
Merge pull request #7224 from choroba/ech194
Solve 194: Digital Clock & Frequency Equalizer by E. Choroba
Diffstat (limited to 'challenge-194')
-rwxr-xr-xchallenge-194/e-choroba/perl/ch-1.pl28
-rwxr-xr-xchallenge-194/e-choroba/perl/ch-2.pl42
2 files changed, 70 insertions, 0 deletions
diff --git a/challenge-194/e-choroba/perl/ch-1.pl b/challenge-194/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..fa61f2a633
--- /dev/null
+++ b/challenge-194/e-choroba/perl/ch-1.pl
@@ -0,0 +1,28 @@
+#! /usr/bin/perl
+use warnings;
+use strict;
+use experimental 'signatures';
+
+sub digital_clock ($pattern) {
+ my $pos = index $pattern, '?';
+
+ my ($regex, $then, $else) = @{
+ {4 => [qr/./, 9],
+ 3 => [qr/./, 5],
+ 1 => [qr/^[01]/, 9, 3],
+ 0 => [qr/^.[4-9]/, 1, 2]
+ }->{$pos}
+ };
+ return $pattern =~ $regex ? $then : $else
+}
+
+use Test::More tests => 6 + 1;
+
+is digital_clock('?5:00'), 1, 'Example 1';
+is digital_clock('?3:00'), 2, 'Example 2';
+is digital_clock('1?:00'), 9, 'Example 3';
+is digital_clock('2?:00'), 3, 'Example 4';
+is digital_clock('12:?5'), 5, 'Example 5';
+is digital_clock('12:5?'), 9, 'Example 6';
+
+is digital_clock('?4:00'), 1, '24 invalid';
diff --git a/challenge-194/e-choroba/perl/ch-2.pl b/challenge-194/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..2e6bacccac
--- /dev/null
+++ b/challenge-194/e-choroba/perl/ch-2.pl
@@ -0,0 +1,42 @@
+#! /usr/bin/perl
+use warnings;
+use strict;
+use experimental 'signatures';
+
+sub frequency_equalizer ($s) {
+ return 0 unless length $s;
+
+ my %freq;
+ ++$freq{$_} for split //, $s;
+ my %by_freq;
+ undef $by_freq{ $freq{$_} }{$_} for keys %freq;
+
+ # More than 2 freqs.
+ return 0 if keys %by_freq > 2;
+
+ # Single character.
+ return 1 if 1 == keys %freq;
+
+ # Too many to remove.
+ my @sizes = sort { $a <=> $b } keys %by_freq;
+ return 0 if 1 == @sizes # xy
+ || $sizes[1] - $sizes[0] != 1 # xyyy
+ || 1 < keys %{ $by_freq{ $sizes[1] } }; # xyyzz
+
+ return 1
+}
+
+use Test::More tests => 3 + 8;
+
+is frequency_equalizer('abbc'), 1, 'Example 1';
+is frequency_equalizer('xyzyyxz'), 1, 'Example 2';
+is frequency_equalizer('xzxz'), 0, 'Example 3';
+
+is frequency_equalizer('aaaabbbcc'), 0, 'More than two freqs';
+is frequency_equalizer('aabbcd'), 0, 'Too many instances';
+is frequency_equalizer('aabbccd'), 0, 'Too many to remove';
+is frequency_equalizer('xxzzzz'), 0, 'Difference too large';
+is frequency_equalizer(""), 0, 'Empty string';
+is frequency_equalizer('a'), 1, 'Single character';
+is frequency_equalizer('aaaa'), 1, 'Single character repeated';
+is frequency_equalizer('abcdd'), 1, 'Many instances';