aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-04-30 23:28:51 +0100
committerGitHub <noreply@github.com>2024-04-30 23:28:51 +0100
commit300d6c91056018395faf9508398561f3416c2e6a (patch)
tree2ccfc2d94a24543b844b2e668860f7affc93ed97
parentaa10d92be68bb71221ab18ecbc75bbd9b56b3c50 (diff)
parent5cb72e307c9bd890b5a917dc8cfaa4fbf05497dc (diff)
downloadperlweeklychallenge-club-300d6c91056018395faf9508398561f3416c2e6a.tar.gz
perlweeklychallenge-club-300d6c91056018395faf9508398561f3416c2e6a.tar.bz2
perlweeklychallenge-club-300d6c91056018395faf9508398561f3416c2e6a.zip
Merge pull request #10015 from pokgopun/pwc267
Pwc267
-rw-r--r--challenge-267/pokgopun/go/ch-1.go74
-rw-r--r--challenge-267/pokgopun/go/ch-2.go88
-rw-r--r--challenge-267/pokgopun/python/ch-1.py60
-rw-r--r--challenge-267/pokgopun/python/ch-2.py67
4 files changed, 289 insertions, 0 deletions
diff --git a/challenge-267/pokgopun/go/ch-1.go b/challenge-267/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..d9fe7d7027
--- /dev/null
+++ b/challenge-267/pokgopun/go/ch-1.go
@@ -0,0 +1,74 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-267/
+/*#
+
+Task 1: Product Sign
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of @ints.
+
+ Write a script to find the sign of product of all integers in the given
+ array. The sign is 1 if the product is positive, -1 if the product is
+ negative and 0 if product is zero.
+
+Example 1
+
+Input: @ints = (-1, -2, -3, -4, 3, 2, 1)
+Output: 1
+
+The product -1 x -2 x -3 x -4 x 3 x 2 x 1 => 144 > 0
+
+Example 2
+
+Input: @ints = (1, 2, 0, -2, -1)
+Output: 0
+
+The product 1 x 2 x 0 x -2 x -1 => 0
+
+Example 3
+
+Input: @ints = (-1, -1, 1, -1, 2)
+Output: -1
+
+The product -1 x -1 x 1 x -1 x 2 => -2 < 0
+
+Task 2: Line Counts
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+type ints []int
+
+func (is ints) productSign() int {
+ res := 1
+ for _, v := range is {
+ if v == 0 {
+ return 0
+ } else if v < 0 {
+ res *= -1
+ }
+ }
+ return res
+}
+
+func main() {
+ for _, data := range []struct {
+ input ints
+ output int
+ }{
+ {ints{-1, -2, -3, -4, 3, 2, 1}, 1},
+ {ints{1, 2, 0, -2, -1}, 0},
+ {ints{-1, -1, 1, -1, 2}, -1},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.input.productSign(), data.output)) // blank if ok, otherwise show the difference
+ }
+}
diff --git a/challenge-267/pokgopun/go/ch-2.go b/challenge-267/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..054ff6b7f4
--- /dev/null
+++ b/challenge-267/pokgopun/go/ch-2.go
@@ -0,0 +1,88 @@
+//# https://theweeklychallenge.org/blog/perl-weekly-challenge-267/
+/*#
+
+Task 2: Line Counts
+
+Submitted by: [43]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string, $str, and a 26-items array @widths containing
+ the width of each character from a to z.
+
+ Write a script to find out the number of lines and the width of the
+ last line needed to display the given string, assuming you can only fit
+ 100 width units on a line.
+
+Example 1
+
+Input: $str = "abcdefghijklmnopqrstuvwxyz"
+ @widths = (10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10
+,10,10,10,10,10)
+Output: (3, 60)
+
+Line 1: abcdefghij (100 pixels)
+Line 2: klmnopqrst (100 pixels)
+Line 3: uvwxyz (60 pixels)
+
+Example 2
+
+Input: $str = "bbbcccdddaaa"
+ @widths = (4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
+10,10,10,10,10)
+Output: (2, 4)
+
+Line 1: bbbcccdddaa (98 pixels)
+Line 2: a (4 pixels)
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 5th May 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+#*/
+//# solution by pokgopun@gmail.com
+
+package main
+
+import (
+ "io"
+ "os"
+
+ "github.com/google/go-cmp/cmp"
+)
+
+const lim int = 100
+
+type Answer struct {
+ Lc, L int
+}
+
+type text string
+
+func (txt text) lc(widths []int) Answer {
+ var l, lc, w int
+ lc = 1
+ var b byte
+ for _, b = range []byte(txt) {
+ w = widths[b-97]
+ l += w
+ if l > lim {
+ lc++
+ l = w
+ }
+ }
+ return Answer{lc, l}
+}
+
+func main() {
+ for _, data := range []struct {
+ str text
+ widths []int
+ ans Answer
+ }{
+ {"abcdefghijklmnopqrstuvwxyz", []int{10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, Answer{3, 60}},
+ {"bbbcccdddaaa", []int{4, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, Answer{2, 4}},
+ } {
+ io.WriteString(os.Stdout, cmp.Diff(data.str.lc(data.widths), data.ans)) // blank if ok,, otherwise show the difference
+ }
+}
diff --git a/challenge-267/pokgopun/python/ch-1.py b/challenge-267/pokgopun/python/ch-1.py
new file mode 100644
index 0000000000..5cf430f90f
--- /dev/null
+++ b/challenge-267/pokgopun/python/ch-1.py
@@ -0,0 +1,60 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-267/
+"""
+
+Task 1: Product Sign
+
+Submitted by: [42]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given an array of @ints.
+
+ Write a script to find the sign of product of all integers in the given
+ array. The sign is 1 if the product is positive, -1 if the product is
+ negative and 0 if product is zero.
+
+Example 1
+
+Input: @ints = (-1, -2, -3, -4, 3, 2, 1)
+Output: 1
+
+The product -1 x -2 x -3 x -4 x 3 x 2 x 1 => 144 > 0
+
+Example 2
+
+Input: @ints = (1, 2, 0, -2, -1)
+Output: 0
+
+The product 1 x 2 x 0 x -2 x -1 => 0
+
+Example 3
+
+Input: @ints = (-1, -1, 1, -1, 2)
+Output: -1
+
+The product -1 x -1 x 1 x -1 x 2 => -2 < 0
+
+Task 2: Line Counts
+"""
+### solution by pokgopun@gmail.com
+
+def productSign(ints: tuple):
+ res = 1
+ for i in range(len(ints)):
+ if ints[i]==0:
+ return 0
+ elif ints[i] < 0:
+ res *= -1
+ return res
+
+import unittest
+
+class TestProductSign(unittest.TestCase):
+ def test(self):
+ for inpt,otpt in {
+ (-1, -2, -3, -4, 3, 2, 1): 1,
+ (1, 2, 0, -2, -1): 0,
+ (-1, -1, 1, -1, 2): -1,
+ }.items():
+ self.assertEqual(productSign(inpt),otpt)
+
+unittest.main()
diff --git a/challenge-267/pokgopun/python/ch-2.py b/challenge-267/pokgopun/python/ch-2.py
new file mode 100644
index 0000000000..6c55b61892
--- /dev/null
+++ b/challenge-267/pokgopun/python/ch-2.py
@@ -0,0 +1,67 @@
+### https://theweeklychallenge.org/blog/perl-weekly-challenge-267/
+"""
+
+Task 2: Line Counts
+
+Submitted by: [43]Mohammad Sajid Anwar
+ __________________________________________________________________
+
+ You are given a string, $str, and a 26-items array @widths containing
+ the width of each character from a to z.
+
+ Write a script to find out the number of lines and the width of the
+ last line needed to display the given string, assuming you can only fit
+ 100 width units on a line.
+
+Example 1
+
+Input: $str = "abcdefghijklmnopqrstuvwxyz"
+ @widths = (10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10
+,10,10,10,10,10)
+Output: (3, 60)
+
+Line 1: abcdefghij (100 pixels)
+Line 2: klmnopqrst (100 pixels)
+Line 3: uvwxyz (60 pixels)
+
+Example 2
+
+Input: $str = "bbbcccdddaaa"
+ @widths = (4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,
+10,10,10,10,10)
+Output: (2, 4)
+
+Line 1: bbbcccdddaa (98 pixels)
+Line 2: a (4 pixels)
+ __________________________________________________________________
+
+ Last date to submit the solution 23:59 (UK Time) Sunday 5th May 2024.
+ __________________________________________________________________
+
+SO WHAT DO YOU THINK ?
+"""
+### solution by pokgopun@gmail.com
+
+lim = 100
+
+def lineCount(string: str, widths: tuple):
+ l, lc = 0, 1
+ for c in string:
+ w = widths[ord(c)-97]
+ l += w
+ if l > lim:
+ lc += 1
+ l = w
+ return (lc,l)
+
+import unittest
+
+class TestLineCount(unittest.TestCase):
+ def test(self):
+ for (string, widths), otpt in {
+ ("abcdefghijklmnopqrstuvwxyz", (10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10)): (3, 60),
+ ("bbbcccdddaaa", (4,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10)): (2, 4),
+ }.items():
+ self.assertEqual(lineCount(string,widths),otpt)
+
+unittest.main()