aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMariano Spadaccini <spadacciniweb@gmail.com>2025-10-09 09:23:20 +0200
committerMariano Spadaccini <spadacciniweb@gmail.com>2025-10-09 09:23:20 +0200
commit237e8825539cf440e122a6737bff6b54eae66cf8 (patch)
treed619907626bfee0602b218c87a3103d17713804c
parente13398f14ec1478982b05d6b0675c1556a129a84 (diff)
downloadperlweeklychallenge-club-237e8825539cf440e122a6737bff6b54eae66cf8.tar.gz
perlweeklychallenge-club-237e8825539cf440e122a6737bff6b54eae66cf8.tar.bz2
perlweeklychallenge-club-237e8825539cf440e122a6737bff6b54eae66cf8.zip
Add ch-1 and ch-2 in Python
-rw-r--r--challenge-342/spadacciniweb/python/ch-1.py62
-rw-r--r--challenge-342/spadacciniweb/python/ch-2.py76
2 files changed, 138 insertions, 0 deletions
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)