aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-10-06 19:38:17 +0100
committerGitHub <noreply@github.com>2025-10-06 19:38:17 +0100
commit00561497b520ab51db9c24daeca38d586364b5fa (patch)
treec34012f697196296ba3353e07ce4f7925169fc98
parentff070a61bb43f3419b62524fa5f73c9d0be3541a (diff)
parent77b30efaae318d0d4fe4b8c1f9bcb89c0f853ffa (diff)
downloadperlweeklychallenge-club-00561497b520ab51db9c24daeca38d586364b5fa.tar.gz
perlweeklychallenge-club-00561497b520ab51db9c24daeca38d586364b5fa.tar.bz2
perlweeklychallenge-club-00561497b520ab51db9c24daeca38d586364b5fa.zip
Merge pull request #12802 from zapwai/branch-for-342
Week 342
-rw-r--r--challenge-342/zapwai/perl/ch-1.pl46
-rw-r--r--challenge-342/zapwai/perl/ch-2.pl34
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);