aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2025-06-10 09:24:11 +0100
committerGitHub <noreply@github.com>2025-06-10 09:24:11 +0100
commitedb07839f2811a090379d17980150a0d3263d248 (patch)
tree7b6b1377d0edac38018544af6178448eb0d0ed53
parentffd290826e81ecea3886cc029be6187d9defb222 (diff)
parent3ec475c280544cdbd0bb5e1a04accabf55105557 (diff)
downloadperlweeklychallenge-club-edb07839f2811a090379d17980150a0d3263d248.tar.gz
perlweeklychallenge-club-edb07839f2811a090379d17980150a0d3263d248.tar.bz2
perlweeklychallenge-club-edb07839f2811a090379d17980150a0d3263d248.zip
Merge pull request #12161 from pokgopun/pwc325
Pwc325
-rw-r--r--challenge-325/pokgopun/go/ch-1.go93
-rw-r--r--challenge-325/pokgopun/go/ch-2.go122
-rw-r--r--challenge-325/pokgopun/python/ch-1.py76
-rw-r--r--challenge-325/pokgopun/python/ch-2.py106
4 files changed, 397 insertions, 0 deletions
diff --git a/challenge-325/pokgopun/go/ch-1.go b/challenge-325/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..9ec1056d7a
--- /dev/null
+++ b/challenge-325/pokgopun/go/ch-1.go
@@ -0,0 +1,93 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-325/
+/*#
+
+Task 1: Consecutive One
+
+Submitted by: [43]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a binary array containing only 0 or/and 1.
+
+ Write a script to find out the maximum consecutive 1 in the given
+ array.
+
+Example 1
+
+Input: @binary = (0, 1, 1, 0, 1, 1, 1)
+Output: 3
+
+Example 2
+
+Input: @binary = (0, 0, 0, 0)
+Output: 0
+
+Example 3
+
+Input: @binary = (1, 0, 1, 0, 1, 1)
+Output: 2
+
+Task 2: Final Price
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type ints []int
+
+func (in ints) process() int {
+ var c, mx int
+ n := len(in)
+ for n > 0 {
+ n--
+ if in[n] == 0 {
+ if c == 0 {
+ continue
+ }
+ if c > mx {
+ //fmt.Printf("mx = %d\n", c)
+ mx = c
+ }
+ if n > 0 && n <= mx {
+ //fmt.Printf("remaining %d cannot exceed max %d\n", n, mx)
+ break
+ }
+ c = 0
+ } else {
+ c++
+ }
+ }
+ if n == 0 && in[n] > 0 {
+ //fmt.Println("final max update")
+ if mx < c {
+ //fmt.Printf("mx = %d\n", c)
+ mx = c
+ }
+ }
+ return mx
+}
+
+func main() {
+ for _, data := range []struct {
+ input ints
+ output int
+ }{
+ {ints{0, 1, 1, 0, 1, 1, 1}, 3},
+ {ints{0, 0, 0, 0}, 0},
+ {ints{1, 0, 1, 0, 1, 1}, 2},
+ {ints{1, 0, 1, 1, 0, 1, 1}, 2},
+ {ints{1, 0, 1, 1, 0, 0, 1, 1}, 2},
+ {ints{0, 1, 1, 1, 0, 1, 1, 1}, 3},
+ {ints{1, 1, 1, 1, 0, 1, 1, 1, 0}, 4},
+ {ints{0, 1, 1, 0, 0, 1, 1, 1, 0}, 3},
+ } {
+ //fmt.Println(data)
+ io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-325/pokgopun/go/ch-2.go b/challenge-325/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..830361216f
--- /dev/null
+++ b/challenge-325/pokgopun/go/ch-2.go
@@ -0,0 +1,122 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-325/
+/*#
+
+Task 2: Final Price
+
+Submitted by: [44]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of item prices.
+
+ Write a script to find out the final price of each items in the given
+ array.
+
+ There is a special discount scheme going on. If there’s an item with a
+ lower or equal price later in the list, you get a discount equal to
+ that later price (the first one you find in order).
+
+Example 1
+
+Input: @prices = (8, 4, 6, 2, 3)
+Output: (4, 2, 4, 2, 3)
+
+Item 0:
+The item price is 8.
+The first time that has price <= current item price is 4.
+Final price = 8 - 4 => 4
+
+Item 1:
+The item price is 4.
+The first time that has price <= current item price is 2.
+Final price = 4 - 2 => 2
+
+Item 2:
+The item price is 6.
+The first time that has price <= current item price is 2.
+Final price = 6 - 2 => 4
+
+Item 3:
+The item price is 2.
+No item has price <= current item price, no discount.
+Final price = 2
+
+Item 4:
+The item price is 3.
+Since it is the last item, so no discount.
+Final price = 3
+
+Example 2
+
+Input: @prices = (1, 2, 3, 4, 5)
+Output: (1, 2, 3, 4, 5)
+
+Example 3
+
+Input: @prices = (7, 1, 1, 5)
+Output: (6, 0, 1, 5)
+
+Item 0:
+The item price is 7.
+The first time that has price <= current item price is 1.
+Final price = 7 - 1 => 6
+
+Item 1:
+The item price is 1.
+The first time that has price <= current item price is 1.
+Final price = 1 - 1 => 0
+
+Item 2:
+The item price is 1.
+No item has price <= current item price, so no discount.
+Final price = 1
+
+Item 3:
+The item price is 5.
+Since it is the last item, so no discount.
+Final price = 5
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 15th June 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+ "slices"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type ints []int
+
+func (in ints) process() ints {
+ out := slices.Clone(in)
+ l := len(out)
+ for i := range l - 1 {
+ for _, v := range out[i+1:] {
+ if out[i] >= v {
+ out[i] -= v
+ break
+ }
+ }
+ }
+ return out
+}
+
+func main() {
+ for _, data := range []struct {
+ input, output ints
+ }{
+ {ints{8, 4, 6, 2, 3}, ints{4, 2, 4, 2, 3}},
+ {ints{1, 2, 3, 4, 5}, ints{1, 2, 3, 4, 5}},
+ {ints{7, 1, 1, 5}, ints{6, 0, 1, 5}},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-325/pokgopun/python/ch-1.py b/challenge-325/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..6b0cc53274
--- /dev/null
+++ b/challenge-325/pokgopun/python/ch-1.py
@@ -0,0 +1,76 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-325/
+"""
+
+Task 1: Consecutive One
+
+Submitted by: [43]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a binary array containing only 0 or/and 1.
+
+ Write a script to find out the maximum consecutive 1 in the given
+ array.
+
+Example 1
+
+Input: @binary = (0, 1, 1, 0, 1, 1, 1)
+Output: 3
+
+Example 2
+
+Input: @binary = (0, 0, 0, 0)
+Output: 0
+
+Example 3
+
+Input: @binary = (1, 0, 1, 0, 1, 1)
+Output: 2
+
+Task 2: Final Price
+"""
+### solution by pokgopun@gmail.com
+
+def co(bins: tuple[int]) -> int:
+ c, mx = 0, 0
+ n = len(bins)
+ while n > 0:
+ n -= 1
+ if bins[n] == 0:
+ if c == 0:
+ continue
+ if c > mx:
+ #print("mx =",c)
+ mx = c
+ if n > 0 and n <= mx:
+ #print(f'remaning {n} cannot make up mx')
+ break
+ c = 0
+ else:
+ c += 1
+ if n ==0 and bins[n] > 0:
+ #print("final mx update")
+ if c > mx:
+ #print("mx =",c)
+ mx = c
+ return mx
+
+import unittest
+
+class TestCo(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ (0, 1, 1, 0, 1, 1, 1): 3,
+ (0, 0, 0, 0): 0,
+ (1, 0, 1, 0, 1, 1): 2,
+ (1, 0, 1, 1, 0, 1, 1): 2,
+ (1, 0, 1, 1, 0, 0, 1, 1): 2,
+ (0, 1, 1, 1, 0, 1, 1, 1): 3,
+ (0, 1, 1, 1, 0, 1, 1, 1,0): 3,
+ (0, 1, 1, 0, 0, 1, 1, 1,0): 3,
+ (1, 1, 1, 0, 0, 1, 1, 1,0): 3,
+ (1, 1, 1, 1, 0, 1, 1, 1,0): 4,
+ }.items():
+ #print(inpt,otpt)
+ self.assertEqual(co(inpt),otpt)
+
+unittest.main()
diff --git a/challenge-325/pokgopun/python/ch-2.py b/challenge-325/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..cd9c183f19
--- /dev/null
+++ b/challenge-325/pokgopun/python/ch-2.py
@@ -0,0 +1,106 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-325/
+"""
+
+Task 2: Final Price
+
+Submitted by: [44]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of item prices.
+
+ Write a script to find out the final price of each items in the given
+ array.
+
+ There is a special discount scheme going on. If there’s an item with a
+ lower or equal price later in the list, you get a discount equal to
+ that later price (the first one you find in order).
+
+Example 1
+
+Input: @prices = (8, 4, 6, 2, 3)
+Output: (4, 2, 4, 2, 3)
+
+Item 0:
+The item price is 8.
+The first time that has price <= current item price is 4.
+Final price = 8 - 4 => 4
+
+Item 1:
+The item price is 4.
+The first time that has price <= current item price is 2.
+Final price = 4 - 2 => 2
+
+Item 2:
+The item price is 6.
+The first time that has price <= current item price is 2.
+Final price = 6 - 2 => 4
+
+Item 3:
+The item price is 2.
+No item has price <= current item price, no discount.
+Final price = 2
+
+Item 4:
+The item price is 3.
+Since it is the last item, so no discount.
+Final price = 3
+
+Example 2
+
+Input: @prices = (1, 2, 3, 4, 5)
+Output: (1, 2, 3, 4, 5)
+
+Example 3
+
+Input: @prices = (7, 1, 1, 5)
+Output: (6, 0, 1, 5)
+
+Item 0:
+The item price is 7.
+The first time that has price <= current item price is 1.
+Final price = 7 - 1 => 6
+
+Item 1:
+The item price is 1.
+The first time that has price <= current item price is 1.
+Final price = 1 - 1 => 0
+
+Item 2:
+The item price is 1.
+No item has price <= current item price, so no discount.
+Final price = 1
+
+Item 3:
+The item price is 5.
+Since it is the last item, so no discount.
+Final price = 5
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 15th June 2025.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+def fp(ints: tuple[int]) -> tuple[int]:
+ lst = list(ints)
+ for i in range(len(lst)-1):
+ for v in lst[i+1:]:
+ if lst[i] >= v:
+ lst[i] -= v
+ break
+ return tuple(lst)
+
+import unittest
+
+class TestFp(unittest.TestCase):
+ def test(self):
+ for inpt, otpt in {
+ (8, 4, 6, 2, 3): (4, 2, 4, 2, 3),
+ (1, 2, 3, 4, 5): (1, 2, 3, 4, 5),
+ (7, 1, 1, 5): (6, 0, 1, 5),
+ }.items():
+ self.assertEqual(fp(inpt),otpt)
+
+unittest.main()