aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-12-06 11:43:50 +0000
committerGitHub <noreply@github.com>2022-12-06 11:43:50 +0000
commitebb7ac2c61f4c48eda36fc770241a053329341e8 (patch)
tree760bbf08b8edaedd3936254604ae38db1956690b
parenteb6e7116eb3f5d7868643d03463218e2faaa8ce4 (diff)
parent904208035eb439c36ce091468fba932a934334f5 (diff)
downloadperlweeklychallenge-club-ebb7ac2c61f4c48eda36fc770241a053329341e8.tar.gz
perlweeklychallenge-club-ebb7ac2c61f4c48eda36fc770241a053329341e8.tar.bz2
perlweeklychallenge-club-ebb7ac2c61f4c48eda36fc770241a053329341e8.zip
Merge pull request #7209 from PerlBoy1967/branch-for-challenge-194
w194 - Task 1 & 2
-rwxr-xr-xchallenge-194/perlboy1967/perl/ch-1.pl50
-rwxr-xr-xchallenge-194/perlboy1967/perl/ch-2.pl40
2 files changed, 90 insertions, 0 deletions
diff --git a/challenge-194/perlboy1967/perl/ch-1.pl b/challenge-194/perlboy1967/perl/ch-1.pl
new file mode 100755
index 0000000000..14813849a8
--- /dev/null
+++ b/challenge-194/perlboy1967/perl/ch-1.pl
@@ -0,0 +1,50 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 194
+ - https://theweeklychallenge.org/blog/perl-weekly-challenge-194/#TASK1
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Digital Clock
+Submitted by: Mohammad S Anwar
+
+You are given time in the format hh:mm with one missing digit.
+
+Write a script to find the highest digit between 0-9 that makes it valid time.
+
+=cut
+
+use v5.16;
+use warnings;
+
+use Test::More;
+
+
+sub highestDigit ($) {
+ if ($_[0] !~ m#^[\d\?][\d\?]:[\d\?][\d\?]$# or
+ ($_[0] =~ y/?/?/) != 1) {
+ return -1;
+ } else {
+ my $i = index($_[0],'?');
+ if (0) { } # Do nothing, just look pretty in code
+ elsif ($i == 0) { return substr($_[0],1,1) < 4 ? 2 : 1 }
+ elsif ($i == 1) { return substr($_[0],0,1) < 2 ? 9 : 3 }
+ elsif ($i == 2) { return -1 }
+ elsif ($i == 3) { return 5 }
+ elsif ($i == 4) { return 9 }
+ }
+}
+
+my %t = qw(
+ ?5:00 1 ?3:00 2 1?:00 9 2?:00 3 12:?5 5 12:5? 9
+ 00?00 -1 ??:00 -1 00:?? -1 0?:?0 -1 ?0:0? -1 ????? -1
+ ?00:00 -1 ???:00 -1 00??00 -1 00??00 -1 00:??0 -1 0?:00? -1
+);
+
+for (sort keys %t) {
+ is(highestDigit($_),$t{$_},"highestDigit('$_')");
+}
+
+done_testing;
diff --git a/challenge-194/perlboy1967/perl/ch-2.pl b/challenge-194/perlboy1967/perl/ch-2.pl
new file mode 100755
index 0000000000..d15d3ceaf1
--- /dev/null
+++ b/challenge-194/perlboy1967/perl/ch-2.pl
@@ -0,0 +1,40 @@
+#!/bin/perl
+
+=pod
+
+The Weekly Challenge - 194
+ - https://theweeklychallenge.org/blog/perl-weekly-challenge-194/#TASK2
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Frequency Equalizer
+Submitted by: Mohammad S Anwar
+
+You are given a string made of alphabetic characters only, a-z.
+
+Write a script to determine whether removing only one character can make
+the frequency of the remaining characters the same.
+
+=cut
+
+use v5.16;
+use warnings;
+
+use List::MoreUtils qw(frequency minmax);
+
+use Test::More;
+
+
+sub freqEqual ($) {
+ my ($min,$max) = minmax(keys{reverse frequency(split//,$_[0])});
+ return $min == $max-1 ? 1 : 0;
+}
+
+
+my %t = qw(abbc 1 xyzyyxz 1 xzxz 0);
+
+for (sort keys %t) {
+ is(freqEqual($_),$t{$_},"freqEqual('$_')");
+}
+
+done_testing;