aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Campbell Smith <pj.campbell.smith@gmail.com>2025-11-18 13:13:37 +0000
committerPeter Campbell Smith <pj.campbell.smith@gmail.com>2025-11-18 13:13:37 +0000
commit0bb3fe5baf2b90c0712b396bbbee63ba79db5bff (patch)
treec253b99c3f24f869764609a2cd1aad8b061a3c0f
parent25572447da12014ff4f20d22dcc3260e8a906f99 (diff)
downloadperlweeklychallenge-club-0bb3fe5baf2b90c0712b396bbbee63ba79db5bff.tar.gz
perlweeklychallenge-club-0bb3fe5baf2b90c0712b396bbbee63ba79db5bff.tar.bz2
perlweeklychallenge-club-0bb3fe5baf2b90c0712b396bbbee63ba79db5bff.zip
Week 348 - String time
-rw-r--r--challenge-348/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-348/peter-campbell-smith/perl/ch-1.pl49
-rwxr-xr-xchallenge-348/peter-campbell-smith/perl/ch-2.pl45
3 files changed, 95 insertions, 0 deletions
diff --git a/challenge-348/peter-campbell-smith/blog.txt b/challenge-348/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..d4739c1686
--- /dev/null
+++ b/challenge-348/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/348
diff --git a/challenge-348/peter-campbell-smith/perl/ch-1.pl b/challenge-348/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..8e4c9b2fd6
--- /dev/null
+++ b/challenge-348/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,49 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-11-17
+use utf8; # Week 348 - task 1 - String alike
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+
+string_alike('rhythm');
+string_alike('artifice');
+string_alike('MOONSOUP');
+string_alike('fairzzzz');
+string_alike('uu');
+string_alike('aeioupaeioupaeiuopaeioupaeioupaeiuop');
+string_alike('123+!£ a $#FF & U (]');
+
+sub string_alike {
+
+ my ($string, $output, $size, $half, @count, $c, $h);
+
+ # initialise
+ $string = $_[0];
+ $output = 'false';
+ @count = (0, 0);
+
+ # check for even length
+ $size = length($string);
+ if (($size & 1) == 0) {
+ $half = $size / 2;
+ $h = 0;
+
+ # count vowels
+ for $c (0 .. $size - 1) {
+ $h = 1 if $c == $half;
+ $count[$h] ++ if substr($string, $c, 1) =~ m|[aeiou]|i;
+ }
+
+ # check for equality
+ if ($count[0] != 0 and $count[0] eq $count[1]) {
+ $output = qq[true - $count[0] vowel] . ($count[0] == 1 ? '' : 's');
+ }
+ }
+
+ say qq[\nInput: '$string'];
+ say qq[Output: $output];
+}
+
diff --git a/challenge-348/peter-campbell-smith/perl/ch-2.pl b/challenge-348/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..8531dfd567
--- /dev/null
+++ b/challenge-348/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-11-17
+use utf8; # Week 348 - task 2 - Convert time
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+
+convert_time('02:30', '02:45');
+convert_time('11:55', '12:15');
+convert_time('09:00', '13:00');
+convert_time('23:45', '00:30');
+convert_time('14:20', '15:25');
+convert_time('00:00', '23:59');
+
+sub convert_time {
+
+ my ($from, $till, @chunks, $from_mins, $till_mins, $gap, $j, $ops, $explain);
+
+ # initialise
+ ($from, $till) = @_;
+ @chunks = (60, 15, 5, 1);
+ $explain = '';
+
+ # convert to minutes since midnight
+ $from_mins = substr($from, 0, 2) * 60 + substr($from, 3, 2);
+ $till_mins = substr($till, 0, 2) * 60 + substr($till, 3, 2);
+ $till_mins += 24 * 60 if $till_mins < $from_mins;
+ $gap = $till_mins - $from_mins;
+
+ # analyse difference
+ $ops = 0;
+ for $j (0 .. 3) {
+ while ($gap >= $chunks[$j]) {
+ $gap -= $chunks[$j];
+ $ops ++;
+ $explain .= qq[$chunks[$j] + ];
+ }
+ }
+
+ say qq[\nInput: \$source = '$from', \$target = '$till'];
+ say qq[Output: $ops : ] . substr($explain, 0, -2);
+}