aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwanderdoc <wanderdoc@users.noreply.github.com>2025-10-08 19:44:07 +0200
committerGitHub <noreply@github.com>2025-10-08 19:44:07 +0200
commit10102afcf768fa417030efc73f0679d221ae6a01 (patch)
treeadfe01c8828cd88ca608fb1e99e6fdf88616bd8d
parent17439607e4350aa10c2b6d7eb6c357077ff6f795 (diff)
downloadperlweeklychallenge-club-10102afcf768fa417030efc73f0679d221ae6a01.tar.gz
perlweeklychallenge-club-10102afcf768fa417030efc73f0679d221ae6a01.tar.bz2
perlweeklychallenge-club-10102afcf768fa417030efc73f0679d221ae6a01.zip
Create ch-2.pl
-rw-r--r--challenge-342/wanderdoc/perl/ch-2.pl86
1 files changed, 86 insertions, 0 deletions
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;
+}