diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2025-10-06 11:35:48 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2025-10-06 11:35:48 +0200 |
| commit | d596d7f15aa7cff9c5d3f7a4a8396af82e076555 (patch) | |
| tree | a1ba59662ae3048b11c1fad2639f997da497d70a | |
| parent | 19226ec8142e8e55790e09bb6059d6a16f20f437 (diff) | |
| download | perlweeklychallenge-club-d596d7f15aa7cff9c5d3f7a4a8396af82e076555.tar.gz perlweeklychallenge-club-d596d7f15aa7cff9c5d3f7a4a8396af82e076555.tar.bz2 perlweeklychallenge-club-d596d7f15aa7cff9c5d3f7a4a8396af82e076555.zip | |
Challenge 342 LK Perl Python
| -rw-r--r-- | challenge-342/lubos-kolouch/perl/ch-1.pl | 126 | ||||
| -rw-r--r-- | challenge-342/lubos-kolouch/perl/ch-2.pl | 117 | ||||
| -rw-r--r-- | challenge-342/lubos-kolouch/python/ch-1.py | 59 | ||||
| -rw-r--r-- | challenge-342/lubos-kolouch/python/ch-2.py | 49 |
4 files changed, 351 insertions, 0 deletions
diff --git a/challenge-342/lubos-kolouch/perl/ch-1.pl b/challenge-342/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..e14afbe215 --- /dev/null +++ b/challenge-342/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,126 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Test::More; + +=head1 NAME + +ch-1.pl - Perl Weekly Challenge - Task 1: Balance String + +=head1 DESCRIPTION + +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. + +=head1 USAGE + +perl ch-1.pl + +=head1 AUTHOR + +Lubos Kolouch + +=cut + +=head1 FUNCTIONS + +=head2 balance_string + + my $formatted = balance_string($str); + +Returns the lexicographically smallest formatted string where no two letters or two digits are adjacent, or empty string if impossible. + +=cut + +sub balance_string { + my ($str) = @_; + + # Separate letters and digits + my @letters = sort grep /[a-z]/, split //, $str; + my @digits = sort grep /\d/, split //, $str; + + # Check if valid rearrangement is possible + my $diff = @letters - @digits; + return "" if $diff > 1 || $diff < -1; + + my @result; + my ( $i, $j ) = ( 0, 0 ); + + # Start with the more frequent type + if ( @letters > @digits ) { + + # Start with letter + while ( $i < @letters || $j < @digits ) { + push @result, $letters[$i] if $i < @letters; + $i++; + push @result, $digits[$j] if $j < @digits; + $j++; + } + } + else { + # Start with digit (or equal counts, digit first for lexicographical order) + while ( $i < @letters || $j < @digits ) { + push @result, $digits[$j] if $j < @digits; + $j++; + push @result, $letters[$i] if $i < @letters; + $i++; + } + } + + return join "", @result; +} + +=head1 TESTS + +=head2 Test Example 1 + +Input: $str = "a0b1c2" + +Output: "0a1b2c" + +=cut + +is( balance_string("a0b1c2"), "0a1b2c", "Example 1: a0b1c2 -> 0a1b2c" ); + +=head2 Test Example 2 + +Input: $str = "abc12" + +Output: "a1b2c" + +=cut + +is( balance_string("abc12"), "a1b2c", "Example 2: abc12 -> a1b2c" ); + +=head2 Test Example 3 + +Input: $str = "0a2b1c3" + +Output: "0a1b2c3" + +=cut + +is( balance_string("0a2b1c3"), "0a1b2c3", "Example 3: 0a2b1c3 -> 0a1b2c3" ); + +=head2 Test Example 4 + +Input: $str = "1a23" + +Output: "" + +=cut + +is( balance_string("1a23"), "", "Example 4: 1a23 -> ''" ); + +=head2 Test Example 5 + +Input: $str = "ab123" + +Output: "1a2b3" + +=cut + +is( balance_string("ab123"), "1a2b3", "Example 5: ab123 -> 1a2b3" ); + +done_testing; diff --git a/challenge-342/lubos-kolouch/perl/ch-2.pl b/challenge-342/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..5b050841f9 --- /dev/null +++ b/challenge-342/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,117 @@ +#!/usr/bin/perl +use strict; +use warnings; +use Test::More; + +=head1 NAME + +ch-2.pl - Perl Weekly Challenge - Task 2: Max Score + +=head1 DESCRIPTION + +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. + +=head1 USAGE + +perl ch-2.pl + +=head1 AUTHOR + +Lubos Kolouch + +=cut + +=head1 FUNCTIONS + +=head2 max_score + + my $score = max_score($str); + +Returns the maximum score after splitting the string into two non-empty substrings. + +=cut + +sub max_score { + my ($str) = @_; + return 0 if length $str < 2; + + my $max_score = 0; + + # Iterate through all possible splits + for my $i ( 1 .. length($str) - 1 ) { + + # Get left and right substrings + my $left = substr( $str, 0, $i ); + my $right = substr( $str, $i ); + + # Count zeros in left + my $zeros_left = () = $left =~ /0/g; + + # Count ones in right + my $ones_right = () = $right =~ /1/g; + + # Compute score + my $score = $zeros_left + $ones_right; + + # Update max score + $max_score = $score if $score > $max_score; + } + + return $max_score; +} + +=head1 TESTS + +=head2 Test Example 1 + +Input: $str = "0011" + +Output: 4 + +=cut + +is( max_score("0011"), 4, "Example 1: 0011 -> 4" ); + +=head2 Test Example 2 + +Input: $str = "0000" + +Output: 3 + +=cut + +is( max_score("0000"), 3, "Example 2: 0000 -> 3" ); + +=head2 Test Example 3 + +Input: $str = "1111" + +Output: 3 + +=cut + +is( max_score("1111"), 3, "Example 3: 1111 -> 3" ); + +=head2 Test Example 4 + +Input: $str = "0101" + +Output: 3 + +=cut + +is( max_score("0101"), 3, "Example 4: 0101 -> 3" ); + +=head2 Test Example 5 + +Input: $str = "011101" + +Output: 5 + +=cut + +is( max_score("011101"), 5, "Example 5: 011101 -> 5" ); + +done_testing; diff --git a/challenge-342/lubos-kolouch/python/ch-1.py b/challenge-342/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..18003592ef --- /dev/null +++ b/challenge-342/lubos-kolouch/python/ch-1.py @@ -0,0 +1,59 @@ +import unittest + + +def balance_string(s): + # Separate letters and digits + letters = sorted([c for c in s if c.isalpha()]) + digits = sorted([c for c in s if c.isdigit()]) + + # Check if valid rearrangement is possible + if abs(len(letters) - len(digits)) > 1: + return "" + + # Initialize result + result = [] + i, j = 0, 0 + + # Start with the more frequent type + if len(letters) > len(digits): + # Start with letter + while i < len(letters) or j < len(digits): + if i < len(letters): + result.append(letters[i]) + i += 1 + if j < len(digits): + result.append(digits[j]) + j += 1 + else: + # Start with digit (or equal counts, digit first for lexicographical order) + while i < len(letters) or j < len(digits): + if j < len(digits): + result.append(digits[j]) + j += 1 + if i < len(letters): + result.append(letters[i]) + i += 1 + + return "".join(result) + + +class TestBalanceString(unittest.TestCase): + + def test_example1(self): + self.assertEqual(balance_string("a0b1c2"), "0a1b2c") + + def test_example2(self): + self.assertEqual(balance_string("abc12"), "a1b2c") + + def test_example3(self): + self.assertEqual(balance_string("0a2b1c3"), "0a1b2c3") + + def test_example4(self): + self.assertEqual(balance_string("1a23"), "") + + def test_example5(self): + self.assertEqual(balance_string("ab123"), "1a2b3") + + +if __name__ == '__main__': + unittest.main() diff --git a/challenge-342/lubos-kolouch/python/ch-2.py b/challenge-342/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..fccbd27b0f --- /dev/null +++ b/challenge-342/lubos-kolouch/python/ch-2.py @@ -0,0 +1,49 @@ +import unittest + + +def max_score(s): + if len(s) < 2: + return 0 # Cannot split into two non-empty substrings + + max_score_val = 0 + # Iterate through all possible splits + for i in range(1, len(s)): + # Get left and right substrings + left = s[:i] + right = s[i:] + + # Count zeros in left + zeros_left = left.count('0') + + # Count ones in right + ones_right = right.count('1') + + # Compute score + score = zeros_left + ones_right + + # Update max score + max_score_val = max(max_score_val, score) + + return max_score_val + + +class TestMaxScore(unittest.TestCase): + + def test_example1(self): + self.assertEqual(max_score("0011"), 4) + + def test_example2(self): + self.assertEqual(max_score("0000"), 3) + + def test_example3(self): + self.assertEqual(max_score("1111"), 3) + + def test_example4(self): + self.assertEqual(max_score("0101"), 3) + + def test_example5(self): + self.assertEqual(max_score("011101"), 5) + + +if __name__ == '__main__': + unittest.main() |
