diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-11-03 16:28:44 +0000 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2025-11-03 16:28:44 +0000 |
| commit | f8b0c995453b3f8f084ce86ed86a1e92cfebfe8e (patch) | |
| tree | 70766886434f7974e4a102200b2e78ffab135a5d /challenge-346/eric-cheung/python | |
| parent | 63e805992e5c9c036383aafd85519ed8259e9339 (diff) | |
| download | perlweeklychallenge-club-f8b0c995453b3f8f084ce86ed86a1e92cfebfe8e.tar.gz perlweeklychallenge-club-f8b0c995453b3f8f084ce86ed86a1e92cfebfe8e.tar.bz2 perlweeklychallenge-club-f8b0c995453b3f8f084ce86ed86a1e92cfebfe8e.zip | |
- Added solutions by Mark Anderson.
- Added solutions by E. Choroba.
- Added solutions by Lubos Kolouch.
- Added solutions by Eric Cheung.
- Added solutions by Andreas Mahnke.
- Added solutions by Mohammad Sajid Anwar.
Diffstat (limited to 'challenge-346/eric-cheung/python')
| -rwxr-xr-x | challenge-346/eric-cheung/python/ch-1.py | 24 | ||||
| -rwxr-xr-x | challenge-346/eric-cheung/python/ch-2.py | 48 |
2 files changed, 72 insertions, 0 deletions
diff --git a/challenge-346/eric-cheung/python/ch-1.py b/challenge-346/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..4ed5e07a06 --- /dev/null +++ b/challenge-346/eric-cheung/python/ch-1.py @@ -0,0 +1,24 @@ +
+## strInput = "(()())" ## Example 1
+## strInput = ")()())" ## Example 2
+## strInput = "((()))()(((()" ## Example 3
+## strInput = "))))((()(" ## Example 4
+strInput = "()(()" ## Example 5
+
+strTemp = strInput
+nLastPosFind = len(strInput)
+arrPos = []
+nCount = 0
+
+while (nPos := strTemp.find("()")) > -1:
+ if nPos > nLastPosFind:
+ arrPos.append(nCount)
+ nCount = 0
+
+ strTemp = strTemp[:nPos] + strTemp[nPos + 2:]
+ nLastPosFind = nPos
+ nCount = nCount + 2
+
+arrPos.append(nCount)
+
+print (max(arrPos))
diff --git a/challenge-346/eric-cheung/python/ch-2.py b/challenge-346/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..9b606a0e23 --- /dev/null +++ b/challenge-346/eric-cheung/python/ch-2.py @@ -0,0 +1,48 @@ +
+from itertools import product
+from re import findall
+
+## Example 1
+## strInput = "123"
+## nTarget = 6
+
+## Example 2
+## strInput = "105"
+## nTarget = 5
+
+## Example 3
+## strInput = "232"
+## nTarget = 8
+
+## Example 4
+## strInput = "1234"
+## nTarget = 10
+
+## Example 5
+strInput = "1001"
+nTarget = 2
+
+arrOutput = []
+
+arrChar = ["", "+", "-", "*"]
+arrCartChar = [arrChar] * (len(strInput) - 1)
+
+strExpr = "%".join([("" if nIndx == 0 else str(nIndx)) + charLoop for nIndx, charLoop in enumerate(list(strInput))])
+
+arrAllList = list(product(*arrCartChar))
+arrToRep = ["%" + str(nIndx) for nIndx in range(1, len(strInput))]
+
+for arrLoop in arrAllList:
+ strTemp = strExpr
+ for strToRep, strNuStr in zip(arrToRep, arrLoop):
+ strTemp = strTemp.replace(strToRep, strNuStr)
+
+ if findall("0[0-9]", strTemp):
+ continue
+
+ if eval(strTemp) != nTarget:
+ continue
+
+ arrOutput.append(strTemp)
+
+print (arrOutput)
|
