perl logo Perl logo (Thanks to Olaf Alders)

The weekly challenge 342 - Task 2: Max Score

  1 #!/usr/bin/env perl
  2 # https://theweeklychallenge.org/blog/perl-weekly-challenge-342/#TASK2
  3 #
  4 # Task 2: Max Score
  5 # =================
  6 #
  7 # You are given a string, $str, containing 0 and 1 only.
  8 #
  9 # Write a script to return the max score after splitting the string into two
 10 # non-empty substrings. The score after splitting a string is the number of
 11 # zeros in the left substring plus the number of ones in the right substring.
 12 #
 13 ## Example 1
 14 ##
 15 ## Input: $str = "0011"
 16 ## Output: 4
 17 ##
 18 ## 1: left = "0", right = "011" => 1 + 2 => 3
 19 ## 2: left = "00", right = "11" => 2 + 2 => 4
 20 ## 3: left = "001", right = "1" => 2 + 1 => 3
 21 #
 22 #
 23 ## Example 2
 24 ##
 25 ## Input: $str = "0000"
 26 ## Output: 3
 27 ##
 28 ## 1: left = "0", right = "000" => 1 + 0 => 1
 29 ## 2: left = "00", right = "00" => 2 + 0 => 2
 30 ## 3: left = "000", right = "0" => 3 + 0 => 3
 31 #
 32 #
 33 ## Example 3
 34 ##
 35 ## Input: $str = "1111"
 36 ## Output: 3
 37 ##
 38 ## 1: left = "1", right = "111" => 0 + 3 => 3
 39 ## 2: left = "11", right = "11" => 0 + 2 => 2
 40 ## 3: left = "111", right = "1" => 0 + 1 => 1
 41 #
 42 #
 43 ## Example 4
 44 ##
 45 ## Input: $str = "0101"
 46 ## Output: 3
 47 ##
 48 ## 1: left = "0", right = "101" => 1 + 2 => 3
 49 ## 2: left = "01", right = "01" => 1 + 1 => 2
 50 ## 3: left = "010", right = "1" => 2 + 1 => 3
 51 #
 52 #
 53 ## Example 5
 54 ##
 55 ## Input: $str = "011101"
 56 ## Output: 5
 57 ##
 58 ## 1: left = "0", right = "11101" => 1 + 4 => 5
 59 ## 2: left = "01", right = "1101" => 1 + 3 => 4
 60 ## 3: left = "011", right = "101" => 1 + 2 => 3
 61 ## 4: left = "0111", right = "01" => 1 + 1 => 2
 62 ## 5: left = "01110", right = "1" => 2 + 1 => 3
 63 #
 64 ############################################################
 65 ##
 66 ## discussion
 67 ##
 68 ############################################################
 69 #
 70 # We turn the string into an array of digits. Then we move
 71 # one digit after another to a second array, using these as
 72 # the left and right substrings. We calculate the score in
 73 # each step and keep the maximum.
 74 
 75 use v5.36;
 76 
 77 max_score("0011");
 78 max_score("0000");
 79 max_score("1111");
 80 max_score("0101");
 81 max_score("011101");
 82 
 83 sub max_score($str) {
 84     say "Input: \"$str\"";
 85     my $max = 0;
 86     my @right = split //, $str;
 87     my @left = ();
 88 
 89     push @left, shift @right;
 90     while(@right) {
 91         my $s = score( \@left, \@right );
 92         $max = $s if $s > $max;
 93         push @left, shift @right;
 94     }
 95 
 96     say "Output: $max";
 97 }
 98 
 99 sub score ( $left, $right ) {
100     my $score = 0;
101     foreach my $elem (@$left) {
102         $score ++ if $elem == 0;
103     }
104     foreach my $elem (@$right) {
105         $score ++ if $elem == 1;
106     }
107     return $score;
108 }