aboutsummaryrefslogtreecommitdiff
path: root/challenge-346
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-11-03 16:28:44 +0000
committerMohammad Sajid Anwar <mohammad.anwar@yahoo.com>2025-11-03 16:28:44 +0000
commitf8b0c995453b3f8f084ce86ed86a1e92cfebfe8e (patch)
tree70766886434f7974e4a102200b2e78ffab135a5d /challenge-346
parent63e805992e5c9c036383aafd85519ed8259e9339 (diff)
downloadperlweeklychallenge-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')
-rwxr-xr-xchallenge-346/eric-cheung/python/ch-1.py24
-rwxr-xr-xchallenge-346/eric-cheung/python/ch-2.py48
-rw-r--r--challenge-346/mohammad-anwar/perl/ch-1.pl41
-rw-r--r--challenge-346/mohammad-anwar/python/ch-1.py36
-rw-r--r--challenge-346/mohammad-anwar/raku/ch-1.raku39
5 files changed, 188 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)
diff --git a/challenge-346/mohammad-anwar/perl/ch-1.pl b/challenge-346/mohammad-anwar/perl/ch-1.pl
new file mode 100644
index 0000000000..0d5848c681
--- /dev/null
+++ b/challenge-346/mohammad-anwar/perl/ch-1.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use Test::More;
+
+my @examples = (
+ { str => '(()())', exp => 6 },
+ { str => ')()())', exp => 4 },
+ { str => '((()))()(((()', exp => 8 },
+ { str => '))))((()(', exp => 2 },
+ { str => '()(()', exp => 2 },
+);
+
+foreach (@examples) {
+ is(valid_longest_parenthesis($_->{str}), $_->{exp});
+}
+
+done_testing;
+
+sub valid_longest_parenthesis {
+ my $s = shift;
+ my @stack = (-1);
+ my $max_len = 0;
+
+ for my $i (0 .. length($s) - 1) {
+ if (substr($s, $i, 1) eq "(") {
+ push @stack, $i;
+ } else {
+ pop @stack;
+ if (@stack) {
+ $max_len = $max_len > ($i - $stack[-1])
+ ? $max_len : ($i - $stack[-1]);
+ } else {
+ push @stack, $i; # New starting point
+ }
+ }
+ }
+
+ return $max_len;
+}
diff --git a/challenge-346/mohammad-anwar/python/ch-1.py b/challenge-346/mohammad-anwar/python/ch-1.py
new file mode 100644
index 0000000000..9bdbf3be9e
--- /dev/null
+++ b/challenge-346/mohammad-anwar/python/ch-1.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python3
+
+def valid_longest_parenthesis(s):
+ stack = [-1]
+ max_len = 0
+
+ for i in range(len(s)):
+ if s[i] == "(":
+ stack.append(i)
+ else:
+ stack.pop()
+ if stack:
+ max_len = max(max_len, i - stack[-1])
+ else:
+ stack.append(i) # New starting point
+
+ return max_len
+
+def test_examples():
+ examples = [
+ {"str": "(()())", "exp": 6},
+ {"str": ")()())", "exp": 4},
+ {"str": "((()))()(((()", "exp": 8},
+ {"str": "))))((()(", "exp": 2},
+ {"str": "()(()", "exp": 2},
+ ]
+
+ for example in examples:
+ result = valid_longest_parenthesis(example["str"])
+ expected = example["exp"]
+ assert result == expected, f"Failed for '{example['str']}': expected {expected}, got {result}"
+ print(f"✓ '{example['str']}' -> {result}")
+
+if __name__ == "__main__":
+ test_examples()
+ print("All tests passed!")
diff --git a/challenge-346/mohammad-anwar/raku/ch-1.raku b/challenge-346/mohammad-anwar/raku/ch-1.raku
new file mode 100644
index 0000000000..31183d0c27
--- /dev/null
+++ b/challenge-346/mohammad-anwar/raku/ch-1.raku
@@ -0,0 +1,39 @@
+#!/usr/bin/env raku
+
+use Test;
+
+my @examples = (
+ { str => '(()())', exp => 6 },
+ { str => ')()())', exp => 4 },
+ { str => '((()))()(((()', exp => 8 },
+ { str => '))))((()(', exp => 2 },
+ { str => '()(()', exp => 2 },
+);
+
+for @examples -> %example {
+ is(valid-longest-parenthesis(%example<str>), %example<exp>);
+}
+
+done-testing;
+
+sub valid-longest-parenthesis(Str $s) {
+ my @stack = (-1);
+ my $max-len = 0;
+
+ for 0 .. $s.chars - 1 -> $i {
+ if $s.substr($i, 1) eq "(" {
+ @stack.push($i);
+ } else {
+ @stack.pop();
+ if @stack.elems > 0 {
+ $max-len = $max-len > ($i - @stack[*-1])
+ ?? $max-len
+ !! ($i - @stack[*-1]);
+ } else {
+ @stack.push($i); # New starting point
+ }
+ }
+ }
+
+ return $max-len;
+}