diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-11-05 14:12:24 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-05 14:12:24 +0000 |
| commit | 2d5efa505de21f262b29e35e02296e1fe52a11da (patch) | |
| tree | 6cf48a99c16ddca4a375ee6a6759fab22c68b2da | |
| parent | 7738e0c9a628e2bee2c760e9a1d29d84599e7bd3 (diff) | |
| parent | 6884187a42bfd9c78e0882674032f83141baa3b4 (diff) | |
| download | perlweeklychallenge-club-2d5efa505de21f262b29e35e02296e1fe52a11da.tar.gz perlweeklychallenge-club-2d5efa505de21f262b29e35e02296e1fe52a11da.tar.bz2 perlweeklychallenge-club-2d5efa505de21f262b29e35e02296e1fe52a11da.zip | |
Merge pull request #12975 from pokgopun/pwc346
Pwc346
| -rw-r--r-- | challenge-346/pokgopun/go/ch-1.go | 115 | ||||
| -rw-r--r-- | challenge-346/pokgopun/go/ch-2.go | 213 | ||||
| -rw-r--r-- | challenge-346/pokgopun/python/ch-1.py | 99 | ||||
| -rw-r--r-- | challenge-346/pokgopun/python/ch-2.py | 107 |
4 files changed, 534 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 + } +} diff --git a/challenge-346/pokgopun/python/ch-1.py b/challenge-346/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..4313e4fd71 --- /dev/null +++ b/challenge-346/pokgopun/python/ch-1.py @@ -0,0 +1,99 @@ +### 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 + +def lp(string: str) -> int: + string = list(string) + mx = 0 + c = 0 + #print(string) + for i in range(len(string)): + #print(string[:i+1]) + if string[i] == '(': + c += 1 + else: + c -= 1 + if c < 0: + c = 0 + else: + j = i + string[j] = '' + while string[j] != '(': + j -= 1 + string[j] = '' + while string[j] == '': + j -= 1 + if j < 0: + break + d = i - j + if mx < d: + mx = d + #print(string) + return mx + +import unittest + +class TestLp(unittest.TestCase): + def test(self): + for inpt, otpt in { + '(()())': 6, + ')()())': 4, + '((()))()(((()': 8, + '))))((()(': 2, + '()(()': 2, + '((()(()()': 4, + '((()(()())': 8, + '()(()()': 4, + '()(()())': 8, + }.items(): + #print(f'input={inpt}, output={otpt}') + self.assertEqual(lp(inpt),otpt) + +unittest.main() diff --git a/challenge-346/pokgopun/python/ch-2.py b/challenge-346/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..af9fe62ec0 --- /dev/null +++ b/challenge-346/pokgopun/python/ch-2.py @@ -0,0 +1,107 @@ +### 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 + +def segCmb(n: int, l: int, tmp: list[int] = []): + if n == 1: + tmp.append(l) + yield tmp + else: + for s in range(1,l): + yield from segCmb(n-1, l-s, tmp+[s]) + +def segStr(seg: list[int], string: str) -> list[str]: + strs: list[str] = [] + i = 0 + for n in seg: + strs.append(string[i:i+n]) + i += n + return strs + +def me(string: str, target: int) -> set[str]: + l = len(string) + if l < 2: + return set() + ops = ('+','-','*') + b = len(ops) + res: set[str] = set() + for n in range(2,l+1): + for seg in segCmb(n, l): + dgts = segStr(seg, string) + if "".join(e.lstrip("0") if len(e) > 1 else e for e in dgts) != string: + continue + evl = [dgts[i//2] if i % 2 == 0 else ops[0] for i in range(2*len(dgts)-1)] + for d in range(b**(n-1)): + i = 2*n-1 + while d > 0: + i -= 2 + evl[i] = ops[d%b] + d //= b + while i > 1: + i -= 2 + evl[i] = ops[0] + evlstr = "".join(evl) + if eval(evlstr) == target: + #print(evlstr) + res.add(evlstr) + return res + +import unittest + +class TestMe(unittest.TestCase): + def test(self): + for (string, target), otpt in { + ("123", 6): {"1*2*3", "1+2+3"}, + ("105", 5): {"1*0+5", "10-5"}, + ("232", 8): {"2*3+2", "2+3*2"}, + ("1234", 10): {"1*2*3+4", "1+2+3+4"}, + ("1001", 2): {"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): {'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): {'1*2*30+4*5*60*7+8*9'}, + }.items(): + self.assertEqual(me(string, target), otpt) + +unittest.main() |
