From e13398f14ec1478982b05d6b0675c1556a129a84 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Tue, 7 Oct 2025 17:29:49 +0200 Subject: Add ch-1 and ch-2 in Perl --- challenge-342/spadacciniweb/perl/ch-1.pl | 70 +++++++++++++++++++++++++ challenge-342/spadacciniweb/perl/ch-2.pl | 88 ++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 challenge-342/spadacciniweb/perl/ch-1.pl create mode 100644 challenge-342/spadacciniweb/perl/ch-2.pl 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; +} -- cgit From 237e8825539cf440e122a6737bff6b54eae66cf8 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Thu, 9 Oct 2025 09:23:20 +0200 Subject: Add ch-1 and ch-2 in Python --- challenge-342/spadacciniweb/python/ch-1.py | 62 ++++++++++++++++++++++++ challenge-342/spadacciniweb/python/ch-2.py | 76 ++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 challenge-342/spadacciniweb/python/ch-1.py create mode 100644 challenge-342/spadacciniweb/python/ch-2.py diff --git a/challenge-342/spadacciniweb/python/ch-1.py b/challenge-342/spadacciniweb/python/ch-1.py new file mode 100644 index 0000000000..59db40fdc9 --- /dev/null +++ b/challenge-342/spadacciniweb/python/ch-1.py @@ -0,0 +1,62 @@ +# 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" + +def balance_string(str): + letters = sorted( [x for x in str if x.isalpha()] ) + figures = sorted( [x for x in str if x.isnumeric()] ) + + balance_string = '' + if abs( len(letters) - len(figures) ) <= 1: + if len(letters) > len(figures): + balance_string += letters[0] + letters = letters[1:] + + for i in range(0, len(letters)): + balance_string += figures[0] + figures = figures[1:] + balance_string += letters[0] + letters = letters[1:] + + if len(figures): + balance_string += figures[0] + + print("'%s' -> '%s'" % ( str, balance_string )) + +if __name__ == "__main__": + 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) diff --git a/challenge-342/spadacciniweb/python/ch-2.py b/challenge-342/spadacciniweb/python/ch-2.py new file mode 100644 index 0000000000..65c0f85a9d --- /dev/null +++ b/challenge-342/spadacciniweb/python/ch-2.py @@ -0,0 +1,76 @@ +# 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 + +def max_score(str): + max_score = 0 + if len(str) > 2: + for i in range(1, len(str)): + left = str[0:i] + right = str[i:] + score = len([x for x in left if x == "0"]) +\ + len([x for x in right if x == "1"]) + if score > max_score: + max_score = score + + print("'%s' -> %d" % (str, max_score) ) + +if __name__ == "__main__": + 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) -- cgit From 9f14a6dc43f47fbd0951630695c5415657ce3dd8 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Thu, 9 Oct 2025 12:08:01 +0200 Subject: Add ch-1 and ch-2 in Ruby --- challenge-342/spadacciniweb/ruby/ch-1.rb | 62 +++++++++++++++++++++++++ challenge-342/spadacciniweb/ruby/ch-2.rb | 80 ++++++++++++++++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 challenge-342/spadacciniweb/ruby/ch-1.rb create mode 100644 challenge-342/spadacciniweb/ruby/ch-2.rb diff --git a/challenge-342/spadacciniweb/ruby/ch-1.rb b/challenge-342/spadacciniweb/ruby/ch-1.rb new file mode 100644 index 0000000000..b456a5e8be --- /dev/null +++ b/challenge-342/spadacciniweb/ruby/ch-1.rb @@ -0,0 +1,62 @@ +# 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" + +def balance_string(str) + letters = str.chars.select { |s| /[a-zA-Z]/ =~ s }.sort + figures = str.chars.select { |s| /\d/ =~ s }.sort + balance_string = "" + + if (letters.length - figures.length).abs <= 1 + if letters.length > figures.length + balance_string += letters.shift + end + + (1..letters.length).each do |_| + balance_string += figures.shift + balance_string += letters.shift + end + if figures.length > 0 + balance_string += figures.shift + end + end + + printf "'%s' -> '%s'\n", str, balance_string +end + +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) diff --git a/challenge-342/spadacciniweb/ruby/ch-2.rb b/challenge-342/spadacciniweb/ruby/ch-2.rb new file mode 100644 index 0000000000..af7264da51 --- /dev/null +++ b/challenge-342/spadacciniweb/ruby/ch-2.rb @@ -0,0 +1,80 @@ +# 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 + +def max_score(str) + max_score = 0 + + if str.length > 2 + (0 .. str.length-2).each do |i| + left = str[0..i] + right = str[i+1..] + score = left.split('').select { |x| x == "0" }.length + + right.split('').select { |x| x == "1" }.length + if score > max_score + max_score = score + end + end + end + + printf "'%s' -> %d\n", str, max_score +end + +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) -- cgit From b0064d58e63c0393d51713a37d5f722bf7b037b1 Mon Sep 17 00:00:00 2001 From: Mariano Spadaccini Date: Thu, 9 Oct 2025 16:43:37 +0200 Subject: Add ch-1 and ch-2 in Go --- challenge-342/spadacciniweb/go/ch-1.go | 83 ++++++++++++++++++++++++++++++ challenge-342/spadacciniweb/go/ch-2.go | 93 ++++++++++++++++++++++++++++++++++ 2 files changed, 176 insertions(+) create mode 100644 challenge-342/spadacciniweb/go/ch-1.go create mode 100644 challenge-342/spadacciniweb/go/ch-2.go diff --git a/challenge-342/spadacciniweb/go/ch-1.go b/challenge-342/spadacciniweb/go/ch-1.go new file mode 100644 index 0000000000..eda002e9b9 --- /dev/null +++ b/challenge-342/spadacciniweb/go/ch-1.go @@ -0,0 +1,83 @@ +/* +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" +*/ + +package main + +import ( + "fmt" + "regexp" + "sort" +) + +func balance_string(str string) { + balance_string := "" + + re := regexp.MustCompile(`[a-zA-Z]`) + letters := re.FindAllString(str, -1) + sort.Strings(letters[:]) + + re = regexp.MustCompile(`\d`) + figures := re.FindAllString(str, -1) + sort.Strings(figures[:]) + + if (len(letters) - len(figures) <= 1) && (len(letters) - len(figures) >= 0) || (len(figures) - len(letters) <= 1) && (len(figures) - len(letters) >= 0) { + if (len(letters) > len(figures)) { + balance_string += letters[0] + letters = letters[1:] + } + for range len(letters) { + balance_string += figures[0] + figures = figures[1:] + balance_string += letters[0] + letters = letters[1:] + + } + if len(figures) > 0 { + balance_string += figures[0] + } + } + + fmt.Printf("'%s' -> '%s'\n", str, balance_string) +} + +func main() { + 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) +} diff --git a/challenge-342/spadacciniweb/go/ch-2.go b/challenge-342/spadacciniweb/go/ch-2.go new file mode 100644 index 0000000000..c32ba35004 --- /dev/null +++ b/challenge-342/spadacciniweb/go/ch-2.go @@ -0,0 +1,93 @@ +/* +# 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 +*/ + +package main + +import ( + "fmt" + "regexp" +) + +func max_score(str string) { + max_score := 0 + re_zeros := regexp.MustCompile(`0`) + re_ones := regexp.MustCompile(`1`) + + if len(str) > 2 { + for i := 1; i <= len(str)-1; i++ { + left := str[:i] + right := str[i:] + score := len( re_zeros.FindAllString( left, -1) ) + + len( re_ones.FindAllString( right, -1) ) + if score > max_score { + max_score = score + } + } + } + + fmt.Printf("'%s' -> %d\n",str, max_score); +} + +func main() { + 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) +} -- cgit