aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-10-08 20:12:20 +0100
committerGitHub <noreply@github.com>2025-10-08 20:12:20 +0100
commit4e9468a37429d92d0777cc31448943128aa219dd (patch)
tree30ba43e62b7a99c3346ec8b0ef31b486e9a2240e
parentabae07b233317716fdab7425068f30d0fd8897ea (diff)
parent10102afcf768fa417030efc73f0679d221ae6a01 (diff)
downloadperlweeklychallenge-club-4e9468a37429d92d0777cc31448943128aa219dd.tar.gz
perlweeklychallenge-club-4e9468a37429d92d0777cc31448943128aa219dd.tar.bz2
perlweeklychallenge-club-4e9468a37429d92d0777cc31448943128aa219dd.zip
Merge pull request #12817 from wanderdoc/master
pwc 342 (wanderdoc)
-rw-r--r--challenge-342/wanderdoc/perl/ch-1.pl82
-rw-r--r--challenge-342/wanderdoc/perl/ch-2.pl86
2 files changed, 168 insertions, 0 deletions
diff --git a/challenge-342/wanderdoc/perl/ch-1.pl b/challenge-342/wanderdoc/perl/ch-1.pl
new file mode 100644
index 0000000000..ce0d957091
--- /dev/null
+++ b/challenge-342/wanderdoc/perl/ch-1.pl
@@ -0,0 +1,82 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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.
+
+Example 1
+
+Input: $str = "a0b1c2"
+Output: "0a1b2c"
+
+
+Example 2
+
+Input: $str = "abc12"
+Output: "a1b2c"
+
+
+Example 3
+
+Input: $str = "0a2b1c3"
+Output: "0a1b2c3"
+
+
+Example 4
+
+Input: $str = "1a23"
+Output: ""
+
+
+Example 5
+
+Input: $str = "ab123"
+Output: "1a2b3"
+=cut
+
+
+
+use List::MoreUtils qw(pairwise);
+use Test2::V0 -no_srand => 1;
+
+is(balance_string('a0b1c2'), '0a1b2c', 'Example 1');
+is(balance_string('abc12'), 'a1b2c', 'Example 2');
+is(balance_string('0a2b1c3'), '0a1b2c3', 'Example 3');
+is(balance_string('1a23'), '', 'Example 4');
+is(balance_string('ab123'), '1a2b3', 'Example 5');
+
+done_testing();
+
+sub balance_string
+{
+ my $str = $_[0];
+ my (@ltr, @num);
+ for my $chr ( split(//, $str) )
+ {
+ if ($chr =~ /[a-z]/)
+ {
+ push @ltr, $chr;
+ }
+ elsif ( $chr =~ /[0-9]/ )
+ {
+ push @num, $chr;
+ }
+ }
+ my $count_nums = scalar(@num);
+ my $count_ltrs = scalar(@ltr);
+ return '' if (abs($count_ltrs - $count_nums) > 1);
+ @ltr = sort { $a cmp $b } @ltr;
+ @num = sort { $a <=> $b } @num;
+ my @output;
+ if ( $count_nums >= $count_ltrs)
+ {
+ @output = pairwise { $a . ($b||'') } @num, @ltr;
+ }
+ else
+ {
+ @output = pairwise { $a . ($b||'') } @ltr, @num;
+ }
+ return join('', @output);
+}
diff --git a/challenge-342/wanderdoc/perl/ch-2.pl b/challenge-342/wanderdoc/perl/ch-2.pl
new file mode 100644
index 0000000000..13ce98fcc4
--- /dev/null
+++ b/challenge-342/wanderdoc/perl/ch-2.pl
@@ -0,0 +1,86 @@
+#!perl
+use strict;
+use warnings FATAL => qw(all);
+
+=prompt
+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.
+
+Example 1
+
+Input: $str = "0011"
+Output: 4
+
+1: left = "0", right = "011" => 1 + 2 => 3
+2: left = "00", right = "11" => 2 + 2 => 4
+3: left = "001", right = "1" => 2 + 1 => 3
+
+
+Example 2
+
+Input: $str = "0000"
+Output: 3
+
+1: left = "0", right = "000" => 1 + 0 => 1
+2: left = "00", right = "00" => 2 + 0 => 2
+3: left = "000", right = "0" => 3 + 0 => 3
+
+
+Example 3
+
+Input: $str = "1111"
+Output: 3
+
+1: left = "1", right = "111" => 0 + 3 => 3
+2: left = "11", right = "11" => 0 + 2 => 2
+3: left = "111", right = "1" => 0 + 1 => 1
+
+
+Example 4
+
+Input: $str = "0101"
+Output: 3
+
+1: left = "0", right = "101" => 1 + 2 => 3
+2: left = "01", right = "01" => 1 + 1 => 2
+3: left = "010", right = "1" => 2 + 1 => 3
+
+
+Example 5
+
+Input: $str = "011101"
+Output: 5
+
+1: left = "0", right = "11101" => 1 + 4 => 5
+2: left = "01", right = "1101" => 1 + 3 => 4
+3: left = "011", right = "101" => 1 + 2 => 3
+4: left = "0111", right = "01" => 1 + 1 => 2
+5: left = "01110", right = "1" => 2 + 1 => 3
+=cut
+
+use Test2::V0 -no_srand => 1;
+is(max_score('0011'), 4, 'Example 1');
+is(max_score('0000'), 3, 'Example 2');
+is(max_score('1111'), 3, 'Example 3');
+is(max_score('0101'), 3, 'Example 4');
+is(max_score('011101'), 5, 'Example 5');
+
+done_testing();
+
+sub max_score
+{
+ my $str = $_[0];
+ if ( $str !~ /0/ or $str !~ /1/ )
+ {
+ return length($str) - 1;
+ }
+ my $max_score = 0;
+ for my $idx ( 1 .. length($str) - 1 )
+ {
+ my $zeros = substr($str, 0, $idx) =~ tr/0/0/;
+ my $ones = substr($str, $idx) =~ tr/1/1/;
+ my $score = $zeros + $ones;
+ $max_score = $max_score >= $score ? $max_score : $score;
+ }
+ return $max_score;
+}