aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMariano Spadaccini <spadacciniweb@gmail.com>2025-10-07 17:29:49 +0200
committerMariano Spadaccini <spadacciniweb@gmail.com>2025-10-07 17:29:49 +0200
commite13398f14ec1478982b05d6b0675c1556a129a84 (patch)
treef60e414b42617408e269f52f10c7de9e02db362f
parent1a03e9b07b5d63dc498822d2811224c4cc077321 (diff)
downloadperlweeklychallenge-club-e13398f14ec1478982b05d6b0675c1556a129a84.tar.gz
perlweeklychallenge-club-e13398f14ec1478982b05d6b0675c1556a129a84.tar.bz2
perlweeklychallenge-club-e13398f14ec1478982b05d6b0675c1556a129a84.zip
Add ch-1 and ch-2 in Perl
-rw-r--r--challenge-342/spadacciniweb/perl/ch-1.pl70
-rw-r--r--challenge-342/spadacciniweb/perl/ch-2.pl88
2 files changed, 158 insertions, 0 deletions
diff --git a/challenge-342/spadacciniweb/perl/ch-1.pl b/challenge-342/spadacciniweb/perl/ch-1.pl
new file mode 100644
index 0000000000..534ec5f519
--- /dev/null
+++ b/challenge-342/spadacciniweb/perl/ch-1.pl
@@ -0,0 +1,70 @@
+#!/usr/bin/env perl
+
+# Task 1: Balance String
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given a string made up of lowercase English letters and digits only.
+# Write a script to format the give string where no letter is followed by another letter and no digit is followed by another digit. If there are multiple valid rearrangements, always return the lexicographically smallest one. Return empty string if it is impossible to format the string.
+#
+# Example 1
+# Input: $str = "a0b1c2"
+# Output: "0a1b2c"
+#
+# Example 2
+# Input: $str = "abc12"
+# Output: "a1b2c"
+#
+# Example 3
+# Input: $str = "0a2b1c3"
+# Output: "0a1b2c3"
+#
+# Example 4
+# Input: $str = "1a23"
+# Output: ""
+#
+# Example 5
+# Input: $str = "ab123"
+# Output: "1a2b3"
+
+use strict;
+use warnings;
+
+my $str = "a0b1c2";
+balance_string($str);
+
+$str = "abc12";
+balance_string($str);
+
+$str = "0a2b1c3";
+balance_string($str);
+
+$str = "1a23";
+balance_string($str);
+
+$str = "ab123";
+balance_string($str);
+
+exit 0;
+
+sub balance_string {
+ my $str = shift;
+
+ my @letters = sort grep /[a-z]/, split //, $str;
+ my @figures = sort grep /\d/, split //, $str;
+
+ my @balance_string = ();
+ if (abs((scalar @letters) - (scalar @figures)) <= 1 ) {
+ if (scalar @letters > scalar @figures) {
+ push @balance_string, shift @letters;
+ }
+ foreach (1 .. scalar @letters) {
+ push @balance_string, shift @figures;
+ push @balance_string, shift @letters;
+ }
+ push @balance_string, shift @figures
+ if scalar @figures;
+ }
+
+ printf "'%s' -> '%s'\n", $str,
+ join '', @balance_string;
+}
diff --git a/challenge-342/spadacciniweb/perl/ch-2.pl b/challenge-342/spadacciniweb/perl/ch-2.pl
new file mode 100644
index 0000000000..ea57646594
--- /dev/null
+++ b/challenge-342/spadacciniweb/perl/ch-2.pl
@@ -0,0 +1,88 @@
+#!/usr/bin/env perl
+
+# Task 2: Max Score
+# Submitted by: Mohammad Sajid Anwar
+#
+# 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
+
+use strict;
+use warnings;
+
+my $str = "0011";
+max_score($str);
+
+$str = "0000";
+max_score($str);
+
+$str = "1111";
+max_score($str);
+
+$str = "0101";
+max_score($str);
+
+$str = "011101";
+max_score($str);
+
+exit 0;
+
+sub max_score {
+ my $str = shift;
+
+ my $max_score = 0;
+ if (length($str) > 2) {
+ foreach my $i (1 .. length($str)-1) {
+ my $left = substr $str, 0, $i;
+ my $right = substr $str, $i;
+ my $score = (scalar map { $_ == 0 ? 1 : () } split //, $left)
+ +
+ (scalar map { $_ == 1 ? 1 : () } split //, $right);
+ $max_score = $score
+ if $score > $max_score;
+ }
+ }
+
+ printf "'%s' -> %d\n", $str, $max_score;
+}