diff options
| -rw-r--r-- | challenge-267/pokgopun/python/ch-1.py | 60 | ||||
| -rw-r--r-- | challenge-267/pokgopun/python/ch-2.py | 67 |
2 files changed, 127 insertions, 0 deletions
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() |
