aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPok <pok@goyangi>2025-09-22 14:21:07 +0700
committerPok <pok@goyangi>2025-09-22 14:21:07 +0700
commit7100c57667e38d961da33d83fc6853aed45f8ecb (patch)
treec2925a8c3b7facd0819c77b70fa4ec775f31241f
parent5cf15d7c39021d8b056efd9c3f889a65feb14d45 (diff)
downloadperlweeklychallenge-club-7100c57667e38d961da33d83fc6853aed45f8ecb.tar.gz
perlweeklychallenge-club-7100c57667e38d961da33d83fc6853aed45f8ecb.tar.bz2
perlweeklychallenge-club-7100c57667e38d961da33d83fc6853aed45f8ecb.zip
pwc340 solution in go
-rw-r--r--challenge-340/pokgopun/go/ch-1.go118
-rw-r--r--challenge-340/pokgopun/go/ch-2.go90
2 files changed, 208 insertions, 0 deletions
diff --git a/challenge-340/pokgopun/go/ch-1.go b/challenge-340/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..652b5c093f
--- /dev/null
+++ b/challenge-340/pokgopun/go/ch-1.go
@@ -0,0 +1,118 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-340/
+/*#
+
+Task 1: Duplicate Removals
+
+Submitted by: [48]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string, $str, consisting of lowercase English letters.
+
+ Write a script to return the final string after all duplicate removals
+ have been made. Repeat duplicate removals on the given string until we
+ no longer can.
+
+ A duplicate removal consists of choosing two adjacent and equal
+ letters and removing them.
+
+Example 1
+
+Input: $str = 'abbaca'
+Output: 'ca'
+
+Step 1: Remove 'bb' => 'aaca'
+Step 2: Remove 'aa' => 'ca'
+
+Example 2
+
+Input: $str = 'azxxzy'
+Output: 'ay'
+
+Step 1: Remove 'xx' => 'azzy'
+Step 2: Remove 'zz' => 'ay'
+
+Example 3
+
+Input: $str = 'aaaaaaaa'
+Output: ''
+
+Step 1: Remove 'aa' => 'aaaaaa'
+Step 2: Remove 'aa' => 'aaaa'
+Step 3: Remove 'aa' => 'aa'
+Step 4: Remove 'aa' => ''
+
+Example 4
+
+Input: $str = 'aabccba'
+Output: 'a'
+
+Step 1: Remove 'aa' => 'bccba'
+Step 2: Remove 'cc' => 'bba'
+Step 3: Remove 'bb' => 'a'
+
+Example 5
+
+Input: $str = 'abcddcba'
+Output: ''
+
+Step 1: Remove 'dd' => 'abccba'
+Step 2: Remove 'cc' => 'abba'
+Step 3: Remove 'bb' => 'aa'
+Step 4: Remove 'aa' => ''
+
+Task 2: Ascending Numbers
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func dr(bs []byte) []byte {
+ l := len(bs)
+ if l < 2 {
+ return bs
+ }
+ i := 0
+ for i < l-1 {
+ if bs[i] == bs[i+1] {
+ copy(bs[i:], bs[i+2:])
+ l -= 2
+ bs = bs[:l]
+ continue
+ }
+ i++
+ }
+ return bs
+}
+
+func DuplicateRemovals(str string) string {
+ bs := []byte(str)
+ for {
+ l := len(bs)
+ bs = dr(bs)
+ if len(bs) == l {
+ break
+ }
+ }
+ return string(bs)
+}
+
+func main() {
+ for _, data := range []struct {
+ input, output string
+ }{
+ {"abbaca", "ca"},
+ {"azxxzy", "ay"},
+ {"aaaaaaaa", ""},
+ {"aabccba", "a"},
+ {"abcddcba", ""},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(DuplicateRemovals(data.input), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-340/pokgopun/go/ch-2.go b/challenge-340/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..af7b8a01e9
--- /dev/null
+++ b/challenge-340/pokgopun/go/ch-2.go
@@ -0,0 +1,90 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-340/
+/*#
+
+Task 2: Ascending Numbers
+
+Submitted by: [49]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string, $str, is a list of tokens separated by a single
+ space. Every token is either a positive number consisting of digits 0-9
+ with no leading zeros, or a word consisting of lowercase English
+ letters.
+
+ Write a script to check if all the numbers in the given string are
+ strictly increasing from left to right.
+
+Example 1
+
+Input: $str = "The cat has 3 kittens 7 toys 10 beds"
+Output: true
+
+Numbers 3, 7, 10 - strictly increasing.
+
+Example 2
+
+Input: $str = 'Alice bought 5 apples 2 oranges 9 bananas'
+Output: false
+
+Example 3
+
+Input: $str = 'I ran 1 mile 2 days 3 weeks 4 months'
+Output: true
+
+Example 4
+
+Input: $str = 'Bob has 10 cars 10 bikes'
+Output: false
+
+Example 5
+
+Input: $str = 'Zero is 0 one is 1 two is 2'
+Output: true
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 28th September
+ 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "regexp"
+ "slices"
+ "strconv"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func an(str string) bool {
+ re := regexp.MustCompile(`\d+`)
+ var s []int
+ for _, v := range re.FindAllStringIndex(str, -1) {
+ n, _ := strconv.Atoi(str[v[0]:v[1]])
+ s = append(s, n)
+ }
+ l := len(s)
+ return slices.IsSorted(s) && len(slices.Compact(s)) == l
+}
+
+func main() {
+ for _, data := range []struct {
+ input string
+ output bool
+ }{
+ {"The cat has 3 kittens 7 toys 10 beds", true},
+ {"Alice bought 5 apples 2 oranges 9 bananas", false},
+ {"I ran 1 mile 2 days 3 weeks 4 months", true},
+ {"Bob has 10 cars 10 bikes", false},
+ {"Zero is 0 one is 1 two is 2", true},
+ } {
+ //fmt.Println(data.input)
+ io.WriteString(os.Stdout, cmp.Diff(an(data.input), data.output)) // blank if ok, otherwise show the difference
+ }
+}