From 77b30efaae318d0d4fe4b8c1f9bcb89c0f853ffa Mon Sep 17 00:00:00 2001 From: David Ferrone Date: Mon, 6 Oct 2025 10:58:28 -0400 Subject: Week 342 --- challenge-342/zapwai/perl/ch-1.pl | 46 +++++++++++++++++++++++++++++++++++++++ challenge-342/zapwai/perl/ch-2.pl | 34 +++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 challenge-342/zapwai/perl/ch-1.pl create mode 100644 challenge-342/zapwai/perl/ch-2.pl diff --git a/challenge-342/zapwai/perl/ch-1.pl b/challenge-342/zapwai/perl/ch-1.pl new file mode 100644 index 0000000000..cc3dea0c9b --- /dev/null +++ b/challenge-342/zapwai/perl/ch-1.pl @@ -0,0 +1,46 @@ +use v5.38; + +sub proc($str) { + say "Input: $str"; + my @num; + my @let; + for my $l (split '', $str) { + if ($l =~ /\d/) { + push @num, $l; + } else { + push @let, $l; + } + } + + if (abs(scalar @num - scalar @let) >= 2) { + say "Output: \"\""; + } else { + my $o; + if (scalar @num > scalar @let) { + $o = $num[0]; + for my $i (0 .. $#let) { + $o = $o.$let[$i].$num[$i+1]; + } + } else { + $o = $let[0]; + for my $i (0 .. $#num) { + $o = $o.$num[$i].$let[$i+1]; + } + } + say "Output: \"$o\""; + } + +} + +my $str = "a0b1c2"; +proc($str); +$str = "abc12"; +proc($str); +$str = "0a1b2c3"; +proc($str); +$str = "1a23"; +proc($str); +$str = "ab123"; +proc($str); + +## Champion of the week! Yay! Thank you PWC and M. Anwar :-) diff --git a/challenge-342/zapwai/perl/ch-2.pl b/challenge-342/zapwai/perl/ch-2.pl new file mode 100644 index 0000000000..3ecf2f2467 --- /dev/null +++ b/challenge-342/zapwai/perl/ch-2.pl @@ -0,0 +1,34 @@ +use v5.38; + +sub proc($str) { + say "Input: $str"; + my $max = 0; + for my $len (1 .. length( $str ) - 1) { + my $pre = substr $str, 0, $len; + my $post = substr $str, $len; + my $left_sum = 0; # Sum of zeros on left + my $right_sum = 0; # Sum of ones on right + for my $n (split '', $pre) { + $left_sum++ if ($n eq "0"); + } + for my $n (split '', $post) { + $right_sum++ if ($n eq "1"); + } + my $total = $left_sum + $right_sum; + if ($max < $total) { + $max = $total; + } + } + say "Output: $max"; +} + +my $str = "0011"; +proc($str); +$str = "0000"; +proc($str); +$str = "1111"; +proc($str); +$str = "0101"; +proc($str); +$str = "011101"; +proc($str); -- cgit