aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-10-06 19:38:51 +0100
committerGitHub <noreply@github.com>2025-10-06 19:38:51 +0100
commit1edfdfa56247be5433d17f40b83d8146a6ef1070 (patch)
tree25c53c1069ddcb1678438acf6ce433c94983cee5
parentdba6dd712a2f1b40e815857c5cdd8cce3b74a35c (diff)
parent49f74fa573d3551a8b1a18ee1e89c5a99f2e50f2 (diff)
downloadperlweeklychallenge-club-1edfdfa56247be5433d17f40b83d8146a6ef1070.tar.gz
perlweeklychallenge-club-1edfdfa56247be5433d17f40b83d8146a6ef1070.tar.bz2
perlweeklychallenge-club-1edfdfa56247be5433d17f40b83d8146a6ef1070.zip
Merge pull request #12804 from pjcs00/wk342
Week 342 - Fun with characters
-rw-r--r--challenge-342/peter-campbell-smith/blog.txt1
-rwxr-xr-xchallenge-342/peter-campbell-smith/perl/ch-1.pl48
-rwxr-xr-xchallenge-342/peter-campbell-smith/perl/ch-2.pl46
3 files changed, 95 insertions, 0 deletions
diff --git a/challenge-342/peter-campbell-smith/blog.txt b/challenge-342/peter-campbell-smith/blog.txt
new file mode 100644
index 0000000000..9fcbd0bb93
--- /dev/null
+++ b/challenge-342/peter-campbell-smith/blog.txt
@@ -0,0 +1 @@
+http://ccgi.campbellsmiths.force9.co.uk/challenge/342
diff --git a/challenge-342/peter-campbell-smith/perl/ch-1.pl b/challenge-342/peter-campbell-smith/perl/ch-1.pl
new file mode 100755
index 0000000000..228022e410
--- /dev/null
+++ b/challenge-342/peter-campbell-smith/perl/ch-1.pl
@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-10-06
+use utf8; # Week 342 - task 1 - Balance string
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+
+balance_string('a0b1c2');
+balance_string('abc12');
+balance_string('0a2b1c3');
+balance_string('1a23');
+balance_string('ab123');
+balance_string('theweeklychallenge987654321098765432');
+balance_string('');
+balance_string('z');
+balance_string('9');
+
+sub balance_string {
+
+ my (@symbols, @letters, @numbers, $c, $result, $diff, $paired);
+
+ # sort string
+ @symbols = sort(split('', $_[0]));
+ @numbers = @letters = ();
+
+ # separate numbers and letters
+ for $c (@symbols) {
+ if ($c lt 'a') { push(@numbers, $c) }
+ else { push(@letters, $c) }
+ }
+
+ # create result
+ $diff = @letters - @numbers;
+ if (abs($diff) <= 1 and @symbols) {
+ $paired = $diff < 0 ? @letters : @numbers;
+ $result = shift(@letters) if $diff == 1;
+ $result .= shift(@numbers) . shift(@letters) for 1 .. $paired;
+ $result .= shift(@numbers) if $diff == -1;
+ } else {
+ $result = '';
+ }
+
+ say qq[\nInput: '$_[0]'];
+ say qq[Output: '$result'];
+}
diff --git a/challenge-342/peter-campbell-smith/perl/ch-2.pl b/challenge-342/peter-campbell-smith/perl/ch-2.pl
new file mode 100755
index 0000000000..371d97ae43
--- /dev/null
+++ b/challenge-342/peter-campbell-smith/perl/ch-2.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/perl
+
+# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge
+
+use v5.26; # The Weekly Challenge - 2025-10-06
+use utf8; # Week 342 - task 2 - Max score
+use warnings; # Peter Campbell Smith
+binmode STDOUT, ':utf8';
+use Encode;
+
+max_score('0011');
+max_score('0000');
+max_score('1111');
+max_score('011101');
+max_score('10');
+
+my $string;
+$string .= int(rand(2)) for 0..99;
+max_score($string);
+
+sub max_score {
+
+ my ($string, @numbers, $score, $best, $j, $explain);
+
+ # initialise
+ $string = shift;
+ @numbers = split('', $string);
+ $best = -1;
+
+ # count all the 1s
+ $score += $_ for @numbers;
+
+ # move the boundary steadily right, adjusting the score
+ for $j (0 .. length($string) - 2) {
+ $score += $numbers[$j] == 1 ? -1 : 1;
+
+ # check if this is the best
+ next unless $score > $best;
+ $best = $score;
+ $explain = substr($string, 0, $j + 1) . '~' . substr($string, $j + 1);
+ }
+
+ # report
+ say qq[\nInput: '$string'];
+ say qq[Output: $best: $explain];
+}