aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-08-28 23:36:07 +0100
committerGitHub <noreply@github.com>2025-08-28 23:36:07 +0100
commit5b329eab3b34e2132988f8797bee4e7106e96d33 (patch)
tree5dc8ff1d7ecf6984e1773d98bfa0910c88cb022a
parentc78205bb18ba5a004a3b4f58953d70a35e05c9cc (diff)
parenta99c5c940e444329ef45f064d101690b9838e58b (diff)
downloadperlweeklychallenge-club-5b329eab3b34e2132988f8797bee4e7106e96d33.tar.gz
perlweeklychallenge-club-5b329eab3b34e2132988f8797bee4e7106e96d33.tar.bz2
perlweeklychallenge-club-5b329eab3b34e2132988f8797bee4e7106e96d33.zip
Merge pull request #12588 from pokgopun/pwc336
Pwc336
-rw-r--r--challenge-336/pokgopun/go/ch-1.go109
-rw-r--r--challenge-336/pokgopun/go/ch-2.go157
-rw-r--r--challenge-336/pokgopun/python/ch-1.py87
-rw-r--r--challenge-336/pokgopun/python/ch-2.py134
4 files changed, 487 insertions, 0 deletions
diff --git a/challenge-336/pokgopun/go/ch-1.go b/challenge-336/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..7d8540aede
--- /dev/null
+++ b/challenge-336/pokgopun/go/ch-1.go
@@ -0,0 +1,109 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-336/
+/*#
+
+Task 1: Equal Group
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of integers.
+
+ Write a script to return true if the given array can be divided into
+ one or more groups: each group must be of the same size as the others,
+ with at least two members, and with all members having the same value.
+
+Example 1
+
+Input: @ints = (1,1,2,2,2,2)
+Output: true
+
+Groups: (1,1), (2,2), (2,2)
+
+Example 2
+
+Input: @ints = (1,1,1,2,2,2,3,3)
+Output: false
+
+Groups: (1,1,1), (2,2,2), (3,3)
+
+Example 3
+
+Input: @ints = (5,5,5,5,5,5,7,7,7,7,7,7)
+Output: true
+
+Groups: (5,5,5,5,5,5), (7,7,7,7,7,7)
+
+Example 4
+
+Input: @ints = (1,2,3,4)
+Output: false
+
+Example 5
+
+Input: @ints = (8,8,9,9,10,10,11,11)
+Output: true
+
+Groups: (8,8), (9,9), (10,10), (11,11)
+
+Task 2: Final Score
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "github.com/google/go-cmp/cmp"
+ "io"
+ "os"
+ "slices"
+)
+
+type ints []int
+
+func (in ints) process() bool {
+ slices.Sort(in)
+ l := len(in)
+ if l < 2 {
+ return false
+ }
+ var res bool
+ for d := 2; d <= l; d++ {
+ if l%d != 0 {
+ continue
+ }
+ res = true
+ for i := d; i <= l; i += d {
+ n := in[i-d]
+ for _, v := range in[i-d+1 : i] {
+ if v != n {
+ res = false
+ goto skip
+ }
+ }
+ }
+ skip:
+ if res == true {
+ break
+ }
+ }
+ return res
+}
+
+func main() {
+ for _, data := range []struct {
+ input ints
+ output bool
+ }{
+ {ints{1, 1, 2, 2, 2, 2}, true},
+ {ints{1, 1, 1, 2, 2, 2, 3, 3}, false},
+ {ints{5, 5, 5, 5, 5, 5, 7, 7, 7, 7, 7, 7}, true},
+ {ints{1, 2, 3, 4}, false},
+ {ints{8, 8, 9, 9, 10, 10, 11, 11}, true},
+ {ints{1, 1}, true},
+ {ints{1, 1, 1, 2, 2, 2, 3, 3, 3}, true},
+ {ints{1, 1, 1, 1, 1, 1, 3, 3, 3}, true},
+ {ints{1, 1, 1, 3, 3, 3, 3, 3, 3}, true},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-336/pokgopun/go/ch-2.go b/challenge-336/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..0d51a7d3d4
--- /dev/null
+++ b/challenge-336/pokgopun/go/ch-2.go
@@ -0,0 +1,157 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-336/
+/*#
+
+Task 2: Final Score
+
+Submitted by: [43]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of scores by a team.
+
+ Write a script to find the total score of the given team. The score can
+ be any integer, +, C or D. The + adds the sum of previous two scores.
+ The score C invalidates the previous score. The score D will double the
+ previous score.
+
+Example 1
+
+Input: @scores = ("5","2","C","D","+")
+Output: 30
+
+Round 1: 5
+Round 2: 5 + 2
+Round 3: 5 (invalidate the previous score 2)
+Round 4: 5 + 10 (double the previous score 5)
+Round 5: 5 + 10 + 15 (sum of previous two scores)
+
+Total Scores: 30
+
+Example 2
+
+Input: @scores = ("5","-2","4","C","D","9","+","+")
+Output: 27
+
+Round 1: 5
+Round 2: 5 + (-2)
+Round 3: 5 + (-2) + 4
+Round 4: 5 + (-2) (invalidate the previous score 4)
+Round 5: 5 + (-2) + (-4) (double the previous score -2)
+Round 6: 5 + (-2) + (-4) + 9
+Round 7: 5 + (-2) + (-4) + 9 + 5 (sum of previous two scores)
+Round 8: 5 + (-2) + (-4) + 9 + 5 + 14 (sum of previous two scores)
+
+Total Scores: 27
+
+Example 3
+
+Input: @scores = ("7","D","D","C","+","3")
+Output: 45
+
+Round 1: 7
+Round 2: 7 + 14 (double the previous score 7)
+Round 3: 7 + 14 + 28 (double the previous score 14)
+Round 4: 7 + 14 (invalidate the previous score 28)
+Round 5: 7 + 14 + 21 (sum of previous two scores)
+Round 6: 7 + 14 + 21 + 3
+
+Total Scores: 45
+
+Example 4
+
+Input: @scores = ("-5","-10","+","D","C","+")
+Output: -55
+
+Round 1: (-5)
+Round 2: (-5) + (-10)
+Round 3: (-5) + (-10) + (-15) (sum of previous two scores)
+Round 4: (-5) + (-10) + (-15) + (-30) (double the previous score -15)
+Round 5: (-5) + (-10) + (-15) (invalidate the previous score -30)
+Round 6: (-5) + (-10) + (-15) + (-25) (sum of previous two scores)
+
+Total Scores: -55
+
+Example 5
+
+Input: @scores = ("3","6","+","D","C","8","+","D","-2","C","+")
+Output: 128
+
+Round 1: 3
+Round 2: 3 + 6
+Round 3: 3 + 6 + 9 (sum of previous two scores)
+Round 4: 3 + 6 + 9 + 18 (double the previous score 9)
+Round 5: 3 + 6 + 9 (invalidate the previous score 18)
+Round 6: 3 + 6 + 9 + 8
+Round 7: 3 + 6 + 9 + 8 + 17 (sum of previous two scores)
+Round 8: 3 + 6 + 9 + 8 + 17 + 34 (double the previous score 17)
+Round 9: 3 + 6 + 9 + 8 + 17 + 34 + (-2)
+Round 10: 3 + 6 + 9 + 8 + 17 + 34 (invalidate the previous score -2)
+Round 11: 3 + 6 + 9 + 8 + 17 + 34 + 51 (sum of previous two scores)
+
+Total Scores: 128
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 31st August
+ 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "strconv"
+ "github.com/google/go-cmp/cmp"
+)
+
+type ints []int
+
+func (in ints) sum() int{
+ sm := 0
+ for _, v := range in{
+ sm += v
+ }
+ return sm
+}
+
+type input []string
+
+func (in input) process() int{
+ l := len(in)
+ ns := make(ints,l)
+ i := 0
+ o := 0
+ for i < l {
+ switch v := in[i]; v{
+ case "+":
+ ns[i+o] = ns[max(0,i-2+o):i+o].sum()
+ case "C":
+ o -= 2
+ case "D":
+ ns[i+o] = 2 * ns[i-1+o]
+ default:
+ n, _ := strconv.Atoi(v)
+ ns[i+o] = n
+ }
+ i++
+ }
+ return ns[:l+o].sum()
+}
+
+func main(){
+ for _, data := range []struct{
+ input input
+ output int
+ }{
+ {input{"5","2","C","D","+"}, 30},
+ {input{"5","-2","4","C","D","9","+","+"}, 27},
+ {input{"7","D","D","C","+","3"}, 45},
+ {input{"-5","-10","+","D","C","+"}, -55},
+ {input{"3","6","+","D","C","8","+","D","-2","C","+"}, 128},
+ }{
+ io.WriteString(os.Stdout, cmp.Diff(data.input.process(),data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-336/pokgopun/python/ch-1.py b/challenge-336/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..660fa75b2e
--- /dev/null
+++ b/challenge-336/pokgopun/python/ch-1.py
@@ -0,0 +1,87 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-336/
+"""
+
+Task 1: Equal Group
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of integers.
+
+ Write a script to return true if the given array can be divided into
+ one or more groups: each group must be of the same size as the others,
+ with at least two members, and with all members having the same value.
+
+Example 1
+
+Input: @ints = (1,1,2,2,2,2)
+Output: true
+
+Groups: (1,1), (2,2), (2,2)
+
+Example 2
+
+Input: @ints = (1,1,1,2,2,2,3,3)
+Output: false
+
+Groups: (1,1,1), (2,2,2), (3,3)
+
+Example 3
+
+Input: @ints = (5,5,5,5,5,5,7,7,7,7,7,7)
+Output: true
+
+Groups: (5,5,5,5,5,5), (7,7,7,7,7,7)
+
+Example 4
+
+Input: @ints = (1,2,3,4)
+Output: false
+
+Example 5
+
+Input: @ints = (8,8,9,9,10,10,11,11)
+Output: true
+
+Groups: (8,8), (9,9), (10,10), (11,11)
+
+Task 2: Final Score
+"""
+### solution by pokgopun@gmail.com
+
+def eg(ints: tuple[int]) -> bool:
+ l = len(ints)
+ if l < 2:
+ return False
+ ints = sorted(ints)
+ for d in range(2,l+1):
+ if l % d != 0:
+ continue
+ #print("d=",d)
+ for i in range(d,l+1,d):
+ #print(ints[i-d:i])
+ if len(set(ints[i-d:i])) != 1:
+ break
+ else:
+ return True
+ return False
+
+import unittest
+
+class TetEg(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ (1,1,2,2,2,2): True,
+ (1,1,1,2,2,2,3,3): False,
+ (5,5,5,5,5,5,7,7,7,7,7,7): True,
+ (1,2,3,4): False,
+ (8,8,9,9,10,10,11,11): True,
+ (1,1): True,
+ (1,1,1,2,2,2,3,3,3): True,
+ (1,1,1,1,1,1,3,3,3): True,
+ (1,1,1,3,3,3,3,3,3): True,
+ }.items():
+ #print(inpt)
+ self.assertEqual(eg(inpt),otpt)
+
+unittest.main()
diff --git a/challenge-336/pokgopun/python/ch-2.py b/challenge-336/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..829c4a9fa5
--- /dev/null
+++ b/challenge-336/pokgopun/python/ch-2.py
@@ -0,0 +1,134 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-336/
+"""
+
+Task 2: Final Score
+
+Submitted by: [43]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of scores by a team.
+
+ Write a script to find the total score of the given team. The score can
+ be any integer, +, C or D. The + adds the sum of previous two scores.
+ The score C invalidates the previous score. The score D will double the
+ previous score.
+
+Example 1
+
+Input: @scores = ("5","2","C","D","+")
+Output: 30
+
+Round 1: 5
+Round 2: 5 + 2
+Round 3: 5 (invalidate the previous score 2)
+Round 4: 5 + 10 (double the previous score 5)
+Round 5: 5 + 10 + 15 (sum of previous two scores)
+
+Total Scores: 30
+
+Example 2
+
+Input: @scores = ("5","-2","4","C","D","9","+","+")
+Output: 27
+
+Round 1: 5
+Round 2: 5 + (-2)
+Round 3: 5 + (-2) + 4
+Round 4: 5 + (-2) (invalidate the previous score 4)
+Round 5: 5 + (-2) + (-4) (double the previous score -2)
+Round 6: 5 + (-2) + (-4) + 9
+Round 7: 5 + (-2) + (-4) + 9 + 5 (sum of previous two scores)
+Round 8: 5 + (-2) + (-4) + 9 + 5 + 14 (sum of previous two scores)
+
+Total Scores: 27
+
+Example 3
+
+Input: @scores = ("7","D","D","C","+","3")
+Output: 45
+
+Round 1: 7
+Round 2: 7 + 14 (double the previous score 7)
+Round 3: 7 + 14 + 28 (double the previous score 14)
+Round 4: 7 + 14 (invalidate the previous score 28)
+Round 5: 7 + 14 + 21 (sum of previous two scores)
+Round 6: 7 + 14 + 21 + 3
+
+Total Scores: 45
+
+Example 4
+
+Input: @scores = ("-5","-10","+","D","C","+")
+Output: -55
+
+Round 1: (-5)
+Round 2: (-5) + (-10)
+Round 3: (-5) + (-10) + (-15) (sum of previous two scores)
+Round 4: (-5) + (-10) + (-15) + (-30) (double the previous score -15)
+Round 5: (-5) + (-10) + (-15) (invalidate the previous score -30)
+Round 6: (-5) + (-10) + (-15) + (-25) (sum of previous two scores)
+
+Total Scores: -55
+
+Example 5
+
+Input: @scores = ("3","6","+","D","C","8","+","D","-2","C","+")
+Output: 128
+
+Round 1: 3
+Round 2: 3 + 6
+Round 3: 3 + 6 + 9 (sum of previous two scores)
+Round 4: 3 + 6 + 9 + 18 (double the previous score 9)
+Round 5: 3 + 6 + 9 (invalidate the previous score 18)
+Round 6: 3 + 6 + 9 + 8
+Round 7: 3 + 6 + 9 + 8 + 17 (sum of previous two scores)
+Round 8: 3 + 6 + 9 + 8 + 17 + 34 (double the previous score 17)
+Round 9: 3 + 6 + 9 + 8 + 17 + 34 + (-2)
+Round 10: 3 + 6 + 9 + 8 + 17 + 34 (invalidate the previous score -2)
+Round 11: 3 + 6 + 9 + 8 + 17 + 34 + 51 (sum of previous two scores)
+
+Total Scores: 128
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 31st August
+ 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def fc(scores: tuple[str]) -> int:
+ scores = list(scores)
+ l = len(scores)
+ i = 0
+ while i < l:
+ match scores[i]:
+ case '+':
+ scores[i] = sum(scores[max(0,i-2):i])
+ case 'C':
+ scores = scores[:i-1] + scores[i+1:]
+ i -= 1
+ l -= 2
+ continue
+ case 'D':
+ scores[i] = 2 * scores[i-1]
+ case _:
+ scores[i] = int(scores[i])
+ i += 1
+ return sum(scores)
+
+import unittest
+
+class TestFc(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ ("5","2","C","D","+"): 30,
+ ("5","-2","4","C","D","9","+","+"): 27,
+ ("7","D","D","C","+","3"): 45,
+ ("-5","-10","+","D","C","+"): -55,
+ ("3","6","+","D","C","8","+","D","-2","C","+"): 128,
+ }.items():
+ self.assertEqual(fc(inpt),otpt)
+
+unittest.main()