aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-10-11 22:54:47 +0100
committerGitHub <noreply@github.com>2025-10-11 22:54:47 +0100
commit7673dc669a1167bb7a8d24df466bc4e701c50785 (patch)
tree51740fb1049e21f6ae4f04c0b6197fa8fa301b1f
parente0098624423f357ff8d86a862d6b639f4025a6d9 (diff)
parent7e1fd8934f8a9f6f9da7f2e3d36ed97bab2cd2b6 (diff)
downloadperlweeklychallenge-club-7673dc669a1167bb7a8d24df466bc4e701c50785.tar.gz
perlweeklychallenge-club-7673dc669a1167bb7a8d24df466bc4e701c50785.tar.bz2
perlweeklychallenge-club-7673dc669a1167bb7a8d24df466bc4e701c50785.zip
Merge pull request #12828 from vinodk89/branch-for-challenge-342
Solutions for Challenge-342 (Perl and Python)
-rw-r--r--challenge-342/vinod-k/perl/ch-1.pl35
-rw-r--r--challenge-342/vinod-k/perl/ch-2.pl32
-rw-r--r--challenge-342/vinod-k/python/ch-1.py34
-rw-r--r--challenge-342/vinod-k/python/ch-2.py19
4 files changed, 120 insertions, 0 deletions
diff --git a/challenge-342/vinod-k/perl/ch-1.pl b/challenge-342/vinod-k/perl/ch-1.pl
new file mode 100644
index 0000000000..90cf4ebacf
--- /dev/null
+++ b/challenge-342/vinod-k/perl/ch-1.pl
@@ -0,0 +1,35 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+sub format_string {
+ my ($str) = @_;
+
+ my @letters = sort grep { /[a-z]/ } split //, $str;
+ my @digits = sort grep { /\d/ } split //, $str;
+
+ return "" if abs(@letters - @digits) > 1;
+
+ # Determine which type starts firs
+ my $start_with_digit = @digits > @letters ? 1 :
+ @digits < @letters ? 0 :
+ ($digits[0] lt $letters[0] ? 1 : 0);
+
+ my @result;
+ while (@letters or @digits) {
+ if ($start_with_digit) {
+ push @result, shift @digits if @digits;
+ push @result, shift @letters if @letters;
+ } else {
+ push @result, shift @letters if @letters;
+ push @result, shift @digits if @digits;
+ }
+ }
+
+ return join "", @result;
+}
+
+for my $str ("a0b1c2", "abc12", "0a2b1c3", "1a23", "ab123") {
+ print "Input: $str\n";
+ print "Output: ", format_string($str), "\n\n";
+}
diff --git a/challenge-342/vinod-k/perl/ch-2.pl b/challenge-342/vinod-k/perl/ch-2.pl
new file mode 100644
index 0000000000..88cb2173c3
--- /dev/null
+++ b/challenge-342/vinod-k/perl/ch-2.pl
@@ -0,0 +1,32 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+
+sub max_score {
+ my ($str) = @_;
+ my $max_score = 0;
+
+ my $total_ones = ($str =~ tr/1//);
+
+ my $left_zeros = 0;
+ my $right_ones = $total_ones;
+
+ for my $i (0 .. length($str) - 2) {
+ my $char = substr($str, $i, 1);
+ if ($char eq '0') {
+ $left_zeros++;
+ } else {
+ $right_ones--;
+ }
+ my $score = $left_zeros + $right_ones;
+ $max_score = $score if $score > $max_score;
+ }
+
+ return $max_score;
+}
+
+my @examples = ("0011", "0000", "1111", "0101", "011101");
+foreach my $example (@examples) {
+ print "Input: \$str = \"$example\"\n";
+ print "Output: ", max_score($example), "\n\n";
+}
diff --git a/challenge-342/vinod-k/python/ch-1.py b/challenge-342/vinod-k/python/ch-1.py
new file mode 100644
index 0000000000..14af9bfeb0
--- /dev/null
+++ b/challenge-342/vinod-k/python/ch-1.py
@@ -0,0 +1,34 @@
+def format_string(s: str) -> str:
+ letters = sorted([ch for ch in s if ch.isalpha()])
+ digits = sorted([ch for ch in s if ch.isdigit()])
+
+ if abs(len(letters) - len(digits)) > 1:
+ return ""
+
+ if len(digits) > len(letters):
+ start_with_digit = True
+ elif len(digits) < len(letters):
+ start_with_digit = False
+ else:
+ start_with_digit = digits[0] < letters[0]
+
+ result = []
+
+ while letters or digits:
+ if start_with_digit:
+ if digits:
+ result.append(digits.pop(0))
+ if letters:
+ result.append(letters.pop(0))
+ else:
+ if letters:
+ result.append(letters.pop(0))
+ if digits:
+ result.append(digits.pop(0))
+
+ return "".join(result)
+
+
+for s in ["a0b1c2", "abc12", "0a2b1c3", "1a23", "ab123"]:
+ print(f"Input: {s}")
+ print(f"Output: {format_string(s)}\n")
diff --git a/challenge-342/vinod-k/python/ch-2.py b/challenge-342/vinod-k/python/ch-2.py
new file mode 100644
index 0000000000..af58460eb4
--- /dev/null
+++ b/challenge-342/vinod-k/python/ch-2.py
@@ -0,0 +1,19 @@
+def max_score(s: str) -> int:
+ max_score = 0
+ left_zeros = 0
+ right_ones = s.count('1')
+
+ for i in range(len(s) - 1):
+ if s[i] == '0':
+ left_zeros += 1
+ else:
+ right_ones -= 1
+ score = left_zeros + right_ones
+ max_score = max(max_score, score)
+
+ return max_score
+
+examples = ["0011", "0000", "1111", "0101", "011101"]
+for example in examples:
+ print(f"Input: {example}")
+ print(f"Output: {max_score(example)}\n")