aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-10-10 17:20:05 +0100
committerGitHub <noreply@github.com>2025-10-10 17:20:05 +0100
commitb5135c3103e852111032ccf4da3f33d5febe93fa (patch)
tree55362b6fa49017e6ecec5a49c67307cd2132933e
parent2cc39c8dbbdbc43657948f7d0d33e7ed31d6a7b6 (diff)
parentb5f8dbfd21c524ba62d6c5a9c24de4b92491ae90 (diff)
downloadperlweeklychallenge-club-b5135c3103e852111032ccf4da3f33d5febe93fa.tar.gz
perlweeklychallenge-club-b5135c3103e852111032ccf4da3f33d5febe93fa.tar.bz2
perlweeklychallenge-club-b5135c3103e852111032ccf4da3f33d5febe93fa.zip
Merge pull request #12823 from PerlBoy1967/branch-for-challenge-342
w342 - Task 1 & 2 (Perl)
-rwxr-xr-xchallenge-342/perlboy1967/perl/ch1.pl51
-rwxr-xr-xchallenge-342/perlboy1967/perl/ch2.pl37
2 files changed, 88 insertions, 0 deletions
diff --git a/challenge-342/perlboy1967/perl/ch1.pl b/challenge-342/perlboy1967/perl/ch1.pl
new file mode 100755
index 0000000000..0846fea377
--- /dev/null
+++ b/challenge-342/perlboy1967/perl/ch1.pl
@@ -0,0 +1,51 @@
+#!/bin/perl
+
+=pod
+
+L<https://theweeklychallenge.org/blog/perl-weekly-challenge-342#TASK1>
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 1: Balance String
+Submitted by: Mohammad Sajid Anwar
+
+You are given a string made up of lowercase English letters and digits only.
+
+Write a script to format the give string where no letter is followed by
+another letter and no digit is followed by another digit. If there are
+multiple valid rearrangements, always return the lexicographically smallest
+one. Return empty string if it is impossible to format the string.
+
+=cut
+
+use Test2::V0 qw(-no_srand);
+use exact 'v5.32', -signatures;
+
+use List::MoreUtils qw(zip);
+
+sub balanceString ($str) {
+ my (@d,@c);
+
+ map { /\d/ ? push(@d,$_) : push(@c,$_) } sort split(//,$str);
+
+ return '' if (abs(@d - @c) > 1);
+
+ if (@c <= @d) {
+ push(@c,'') if @c != @d;
+ return join '', zip(@d,@c);
+ } elsif (@c > @d) {
+ push(@d,'');
+ return join '', zip(@c,@d);
+ }
+}
+
+is(balanceString('a0b1c2'),'0a1b2c','Example 1');
+is(balanceString('abc12'),'a1b2c','Example 2');
+is(balanceString('0a2b1c3'),'0a1b2c3','Example 3');
+is(balanceString('1a23'),'','Example 4');
+is(balanceString('ab123'),'1a2b3','Example 5');
+is(balanceString('a'),'a','Own example 1');
+is(balanceString('1'),'1','Own example 2');
+is(balanceString('zyx321'),'1x2y3z','Own example 3');
+
+done_testing;
diff --git a/challenge-342/perlboy1967/perl/ch2.pl b/challenge-342/perlboy1967/perl/ch2.pl
new file mode 100755
index 0000000000..68c9922647
--- /dev/null
+++ b/challenge-342/perlboy1967/perl/ch2.pl
@@ -0,0 +1,37 @@
+#!/bin/perl
+
+=pod
+
+L<https://theweeklychallenge.org/blog/perl-weekly-challenge-342#TASK2>
+
+Author: Niels 'PerlBoy' van Dijke
+
+Task 2: Max Score
+Submitted by: Mohammad Sajid Anwar
+
+You are given a string, $str, containing 0 and 1 only.
+
+Write a script to return the max score after splitting the string into
+two non-empty substrings. The score after splitting a string is the number
+of zeros in the left substring plus the number of ones in the right substring.
+
+=cut
+
+use Test2::V0 qw(-no_srand);
+use exact 'v5.32', -signatures;
+
+use List::Util qw(max);
+
+sub maxScore ($str) {
+ max map {
+ substr($str,0,$_) =~ tr/0/0/ + substr($str,$_) =~ tr/1/1/
+ } (1 .. length($str) - 1);
+}
+
+is(maxScore('0011'),4,'Example 1');
+is(maxScore('0000'),3,'Example 2');
+is(maxScore('1111'),3,'Example 3');
+is(maxScore('0101'),3,'Example 4');
+is(maxScore('011101'),5,'Example 5');
+
+done_testing;