aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-346/pokgopun/python/ch-1.py99
-rw-r--r--challenge-346/pokgopun/python/ch-2.py107
2 files changed, 206 insertions, 0 deletions
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()