aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMariano Spadaccini <spadacciniweb@gmail.com>2025-10-09 16:43:37 +0200
committerMariano Spadaccini <spadacciniweb@gmail.com>2025-10-09 16:43:37 +0200
commitb0064d58e63c0393d51713a37d5f722bf7b037b1 (patch)
tree70372a6b6b12a0cefd3b31cad55c336d13c46740
parent9f14a6dc43f47fbd0951630695c5415657ce3dd8 (diff)
downloadperlweeklychallenge-club-b0064d58e63c0393d51713a37d5f722bf7b037b1.tar.gz
perlweeklychallenge-club-b0064d58e63c0393d51713a37d5f722bf7b037b1.tar.bz2
perlweeklychallenge-club-b0064d58e63c0393d51713a37d5f722bf7b037b1.zip
Add ch-1 and ch-2 in Go
-rw-r--r--challenge-342/spadacciniweb/go/ch-1.go83
-rw-r--r--challenge-342/spadacciniweb/go/ch-2.go93
2 files changed, 176 insertions, 0 deletions
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)
+}