diff options
| author | David Ferrone <zapwai@gmail.com> | 2025-10-06 10:58:28 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-06 10:58:28 -0400 |
| commit | 77b30efaae318d0d4fe4b8c1f9bcb89c0f853ffa (patch) | |
| tree | 51af8931b8c3538de5431152c8f64717ef63e91d | |
| parent | 19226ec8142e8e55790e09bb6059d6a16f20f437 (diff) | |
| download | perlweeklychallenge-club-77b30efaae318d0d4fe4b8c1f9bcb89c0f853ffa.tar.gz perlweeklychallenge-club-77b30efaae318d0d4fe4b8c1f9bcb89c0f853ffa.tar.bz2 perlweeklychallenge-club-77b30efaae318d0d4fe4b8c1f9bcb89c0f853ffa.zip | |
Week 342
| -rw-r--r-- | challenge-342/zapwai/perl/ch-1.pl | 46 | ||||
| -rw-r--r-- | challenge-342/zapwai/perl/ch-2.pl | 34 |
2 files changed, 80 insertions, 0 deletions
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); |
