aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPok <pok@goyangi>2025-11-05 21:01:26 +0700
committerPok <pok@goyangi>2025-11-05 21:01:26 +0700
commit6884187a42bfd9c78e0882674032f83141baa3b4 (patch)
treeb6f48d562250c58dcf6381c7483ba87beb10ddf2
parent5facd2ea57dfc7a39202135b0f342bce355c1583 (diff)
downloadperlweeklychallenge-club-6884187a42bfd9c78e0882674032f83141baa3b4.tar.gz
perlweeklychallenge-club-6884187a42bfd9c78e0882674032f83141baa3b4.tar.bz2
perlweeklychallenge-club-6884187a42bfd9c78e0882674032f83141baa3b4.zip
pwc346 solution in go
-rw-r--r--challenge-346/pokgopun/go/ch-1.go115
-rw-r--r--challenge-346/pokgopun/go/ch-2.go213
2 files changed, 328 insertions, 0 deletions
diff --git a/challenge-346/pokgopun/go/ch-1.go b/challenge-346/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..40458e5cda
--- /dev/null
+++ b/challenge-346/pokgopun/go/ch-1.go
@@ -0,0 +1,115 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-346/
+/*#
+
+Task 1: Longest Parenthesis
+
+Submitted by: [44]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string containing only ( and ).
+
+ Write a script to find the length of the longest valid parenthesis.
+
+Example 1
+
+Input: $str = '(()())'
+Output: 6
+
+Valid Parenthesis: '(()())'
+
+Example 2
+
+Input: $str = ')()())'
+Output: 4
+
+Valid Parenthesis: '()()' at positions 1-4.
+
+Example 3
+
+Input: $str = '((()))()(((()'
+Output: 8
+
+Valid Parenthesis: '((()))()' at positions 0-7.
+
+Example 4
+
+Input: $str = '))))((()('
+Output: 2
+
+Valid Parenthesis: '()' at positions 6-7.
+
+Example 5
+
+Input: $str = '()(()'
+Output: 2
+
+Valid Parenthesis: '()' at positions 0-1 and 3-4.
+
+Task 2: Magic Expression
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+func lp(s string) int {
+ r := []rune(s)
+ var c, mx int
+ //fmt.Println(r)
+ for i := range len(r) {
+ //fmt.Println(r[:i+1])
+ switch r[i] {
+ case '(':
+ c++
+ default:
+ c--
+ if c < 0 {
+ c = 0
+ } else {
+ j := i
+ r[j] = 0
+ for r[j] != '(' {
+ j--
+ }
+ r[j] = 0
+ for r[j] == 0 {
+ j--
+ if j < 0 {
+ break
+ }
+ }
+ d := i - j
+ if mx < d {
+ mx = d
+ }
+ }
+ }
+ }
+ //fmt.Println(r, mx)
+ return mx
+}
+
+func main() {
+ for _, data := range []struct {
+ input string
+ output int
+ }{
+ {"(()())", 6},
+ {")()())", 4},
+ {"((()))()(((()", 8},
+ {"))))((()(", 2},
+ {"()(()", 2},
+ {"((()(()()", 4},
+ {"((()(()())", 8},
+ {"()(()()", 4},
+ {"()(()())", 8},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(lp(data.input), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-346/pokgopun/go/ch-2.go b/challenge-346/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..fd1973cdb8
--- /dev/null
+++ b/challenge-346/pokgopun/go/ch-2.go
@@ -0,0 +1,213 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-346/
+/*#
+
+Task 2: Magic Expression
+
+Submitted by: [45]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string containing only digits and a target integer.
+
+ Write a script to insert binary operators +, - and * between the digits
+ in the given string that evaluates to target integer.
+
+Example 1
+
+Input: $str = "123", $target = 6
+Output: ("1*2*3", "1+2+3")
+
+Example 2
+
+Input: $str = "105", $target = 5
+Output: ("1*0+5", "10-5")
+
+Example 3
+
+Input: $str = "232", $target = 8
+Output: ("2*3+2", "2+3*2")
+
+Example 4
+
+Input: $str = "1234", $target = 10
+Output: ("1*2*3+4", "1+2+3+4")
+
+Example 5
+
+Input: $str = "1001", $target = 2
+Output: ("1+0*0+1", "1+0+0+1", "1+0-0+1", "1-0*0+1", "1-0+0+1", "1-0-0+1")
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 9th November
+ 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "go/ast"
+ "go/parser"
+ "go/token"
+ "io"
+ "iter"
+ "os"
+ "strconv"
+ "strings"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type SegmentCombo iter.Seq[[]int]
+
+func (sc SegmentCombo) process(n, l int, s []int, yield func([]int) bool) {
+ if n == 1 {
+ if !yield(append(s, l)) {
+ return
+ }
+ } else {
+ for i := 1; i < l; i++ {
+ sc.process(n-1, l-i, append(s, i), yield)
+ }
+ }
+}
+
+func segCmb(n, l int) SegmentCombo {
+ return func(yield func([]int) bool) {
+ new(SegmentCombo).process(n, l, []int{}, yield)
+ }
+}
+
+func evlCmb(n int, dgtStr string) iter.Seq[[]string] {
+ return func(yield func([]string) bool) {
+ for seg := range segCmb(n, len(dgtStr)) {
+ l := len(seg)
+ var strs []string
+ i := 0
+ for _, v := range seg {
+ s := dgtStr[i : i+v]
+ i += v
+ if len(s) > 1 && s[0] == 48 {
+ break
+ }
+ l--
+ if l == 0 {
+ strs = append(strs, s)
+ } else {
+ strs = append(strs, s, "+")
+ }
+ }
+ if l == 0 {
+ if !yield(strs) {
+ return
+ }
+ }
+ }
+ }
+}
+
+func bPowerN(b, n int) int {
+ v := b
+ for n > 1 {
+ v *= b
+ n--
+ }
+ return v
+}
+
+func Me(s []string) map[string]struct{} {
+ res := make(map[string]struct{})
+ for _, v := range s {
+ res[v] = struct{}{}
+ }
+ return res
+}
+
+func me(dgtStr string, target int) map[string]struct{} {
+ l := len(dgtStr)
+ if l < 2 {
+ return nil
+ }
+ ops := []rune{'+', '-', '*'}
+ b := len(ops)
+ res := make(map[string]struct{})
+ for n := 2; n < l+1; n++ {
+ for evl := range evlCmb(n, dgtStr) {
+ for d := range bPowerN(b, n-1) {
+ i := 2*n - 1
+ for d > 0 {
+ i -= 2
+ evl[i] = string(ops[d%b])
+ d /= b
+ }
+ for i > 1 {
+ i -= 2
+ evl[i] = string(ops[0])
+ }
+ evlStr := strings.Join(evl, "")
+ evlRes, _ := eval(evlStr)
+ if evlRes == target {
+ res[evlStr] = struct{}{}
+ }
+ }
+ }
+ }
+ //fmt.Println(res)
+ return res
+}
+
+func Eval(exp ast.Expr) int {
+ switch exp := exp.(type) {
+ case *ast.BinaryExpr:
+ return EvalBinaryExpr(exp)
+ case *ast.BasicLit:
+ switch exp.Kind {
+ case token.INT:
+ i, _ := strconv.Atoi(exp.Value)
+ return i
+ }
+ }
+ return 0
+}
+func EvalBinaryExpr(exp *ast.BinaryExpr) int {
+ left := Eval(exp.X)
+ right := Eval(exp.Y)
+ switch exp.Op {
+ case token.ADD:
+ return left + right
+ case token.SUB:
+ return left - right
+ case token.MUL:
+ return left * right
+ case token.QUO:
+ return left / right
+ }
+ return 0
+}
+func eval(expStr string) (int, error) {
+ exp, err := parser.ParseExpr(expStr)
+ if err != nil {
+ return 0, err
+ }
+ return Eval(exp), nil
+}
+
+func main() {
+ for _, data := range []struct {
+ dgtStr string
+ target int
+ output []string
+ }{
+ {"123", 6, []string{"1*2*3", "1+2+3"}},
+ {"105", 5, []string{"1*0+5", "10-5"}},
+ {"232", 8, []string{"2*3+2", "2+3*2"}},
+ {"1234", 10, []string{"1*2*3+4", "1+2+3+4"}},
+ {"1001", 2, []string{"1+0*0+1", "1+0+0+1", "1+0-0+1", "1-0*0+1", "1-0+0+1", "1-0-0+1"}},
+ //{"1234567890", 1979, []string{"1+2*3+45*6*7-8+90", "1+2-3+45*6*7+89-0", "1+2-3+45*6*7+89+0"}},
+ //{"12304560789", 8532, []string{"1*2*30+4*5*60*7+8*9"}},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(me(data.dgtStr, data.target), Me(data.output))) // blank if ok, otherwise show the difference
+ }
+}