diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2025-08-04 23:39:24 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-04 23:39:24 +0100 |
| commit | e487d903afdff70e0e5868e4706fdf38bc39a4ce (patch) | |
| tree | 73c78e7b97bea9f571e1d3a8e8bd1414a3475594 | |
| parent | ccf000132044d76f3fef84f01f797af04f51bce9 (diff) | |
| parent | 672769b6bfa365e497069e98347d69bb65666f4b (diff) | |
| download | perlweeklychallenge-club-e487d903afdff70e0e5868e4706fdf38bc39a4ce.tar.gz perlweeklychallenge-club-e487d903afdff70e0e5868e4706fdf38bc39a4ce.tar.bz2 perlweeklychallenge-club-e487d903afdff70e0e5868e4706fdf38bc39a4ce.zip | |
Merge pull request #12463 from pokgopun/pwc333
Pwc333
| -rw-r--r-- | challenge-333/pokgopun/go/ch-1.go | 96 | ||||
| -rw-r--r-- | challenge-333/pokgopun/go/ch-2.go | 91 | ||||
| -rw-r--r-- | challenge-333/pokgopun/python/ch-1.py | 85 | ||||
| -rw-r--r-- | challenge-333/pokgopun/python/ch-2.py | 78 |
4 files changed, 350 insertions, 0 deletions
diff --git a/challenge-333/pokgopun/go/ch-1.go b/challenge-333/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..9f169ec6b6 --- /dev/null +++ b/challenge-333/pokgopun/go/ch-1.go @@ -0,0 +1,96 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-333/ +/*# + +Task 1: Straight Line + +Submitted by: [43]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a list of co-ordinates. + + Write a script to find out if the given points make a straight line. + +Example 1 + +Input: @list = ([2, 1], [2, 3], [2, 5]) +Output: true + +Example 2 + +Input: @list = ([1, 4], [3, 4], [10, 4]) +Output: true + +Example 3 + +Input: @list = ([0, 0], [1, 1], [2, 3]) +Output: false + +Example 4 + +Input: @list = ([1, 1], [1, 1], [1, 1]) +Output: true + +Example 5 + +Input: @list = ([1000000, 1000000], [2000000, 2000000], [3000000, 3000000]) +Output: true + +Task 2: Duplicate Zeros +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "github.com/google/go-cmp/cmp" + "io" + "os" +) + +type Point struct { + x, y int +} + +func (pnt Point) Slope(p Point) Slope { + return Slope{pnt.y - p.y, pnt.x - p.x} +} + +type Slope struct { + y, x int +} + +func (slp Slope) IsEqual(s Slope) bool { + return slp.x*s.y == slp.y*s.x +} + +type Input []Point + +func (in Input) process() bool { + i := len(in) - 1 + if i < 2 { + return true + } + s0 := in[0].Slope(in[1]) + for i > 1 { + if !s0.IsEqual(in[0].Slope(in[i])){ + return false + } + i-- + } + return true +} + +func main() { + for _, data := range []struct { + input Input + output bool + }{ + {Input{Point{2, 1}, Point{2, 3}, Point{2, 5}}, true}, + {Input{Point{1, 4}, Point{3, 4}, Point{10, 4}}, true}, + {Input{Point{0, 0}, Point{1, 1}, Point{2, 3}}, false}, + {Input{Point{1, 1}, Point{1, 1}, Point{1, 1}}, true}, + {Input{Point{1000000, 1000000}, Point{2000000, 2000000}, Point{3000000, 3000000}}, true}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-333/pokgopun/go/ch-2.go b/challenge-333/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..47961bfa7c --- /dev/null +++ b/challenge-333/pokgopun/go/ch-2.go @@ -0,0 +1,91 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-333/ +/*# + +Task 2: Duplicate Zeros + +Submitted by: [44]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers. + + Write a script to duplicate each occurrence of zero, shifting the + remaining elements to the right. The elements beyond the length of the + original array are not written. + +Example 1 + +Input: @ints = (1, 0, 2, 3, 0, 4, 5, 0) +Output: (1, 0, 0, 2, 3, 0, 0, 4) + +Each zero is duplicated. +Elements beyond the original length (like 5 and last 0) are discarded. + +Example 2 + +Input: @ints = (1, 2, 3) +Output: (1, 2, 3) + +No zeros exist, so the array remains unchanged. + +Example 3 + +Input: @ints = (1, 2, 3, 0) +Output: (1, 2, 3, 0) + +Example 4 + +Input: @ints = (0, 0, 1, 2) +Output: (0, 0, 0, 0) + +Example 5 + +Input: @ints = (1, 2, 0, 3, 4) +Output: (1, 2, 0, 0, 3) + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 10th August + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "github.com/google/go-cmp/cmp" + "io" + "os" + "slices" +) + +type ints []int + +func (in ints) dz() ints { + l := len(in) - 1 + i := 0 + for i < l { + if in[i] == 0 { + in = slices.Insert(in, i, 0) + i++ + } + i++ + } + return in[:l+1] +} + +func main() { + for _, data := range []struct { + input, output ints + }{ + {ints{1, 0, 2, 3, 0, 4, 5, 0}, ints{1, 0, 0, 2, 3, 0, 0, 4}}, + {ints{1, 2, 3}, ints{1, 2, 3}}, + {ints{1, 2, 3, 0}, ints{1, 2, 3, 0}}, + {ints{0, 0, 1, 2}, ints{0, 0, 0, 0}}, + {ints{1, 2, 0, 3, 4}, ints{1, 2, 0, 0, 3}}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.dz(), data.output)) // blank if ok, otherwise show the differeice + + } +} diff --git a/challenge-333/pokgopun/python/ch-1.py b/challenge-333/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..8a12dc5454 --- /dev/null +++ b/challenge-333/pokgopun/python/ch-1.py @@ -0,0 +1,85 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-333/ +""" + +Task 1: Straight Line + +Submitted by: [43]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given a list of co-ordinates. + + Write a script to find out if the given points make a straight line. + +Example 1 + +Input: @list = ([2, 1], [2, 3], [2, 5]) +Output: true + +Example 2 + +Input: @list = ([1, 4], [3, 4], [10, 4]) +Output: true + +Example 3 + +Input: @list = ([0, 0], [1, 1], [2, 3]) +Output: false + +Example 4 + +Input: @list = ([1, 1], [1, 1], [1, 1]) +Output: true + +Example 5 + +Input: @list = ([1000000, 1000000], [2000000, 2000000], [3000000, 3000000]) +Output: true + +Task 2: Duplicate Zeros +""" +### solution by pokgopun@gmail.com + +from typing import Self + +class Point: + def __init__(self, x: int, y: int): + self.x = x + self.y = y + def slope(self, p: Self) -> Self: + return Slope(self.y - p.y, self.x - p.x) + +class Slope: + def __init__(self, y: int, x: int): + self.y = y + self.x = x + def isEqual(self, s: Self) -> bool: + return self.x * s.y == self.y * s.x + +def sl(lst: tuple[tuple[int]]) -> bool: + i = len(lst) - 1 + if i < 2: + return true + p0 = Point(lst[0][0],lst[0][1]) + p1 = Point(lst[1][0],lst[1][1]) + s0 = p0.slope(p1) + while i > 1: + p = Point(lst[i][0],lst[i][1]) + if not s0.isEqual(p0.slope(p)): + return False + i -= 1 + return True + +import unittest + +class TestSl(unittest.TestCase): + def test(self): + for inpt, otpt in { + ((2, 1), (2, 3), (2, 5)): True, + ((1, 4), (3, 4), (10, 4)): True, + ((0, 0), (1, 1), (2, 3)): False, + ((1, 1), (1, 1), (1, 1)): True, + ((1000000, 1000000), (2000000, 2000000), (3000000, 3000000)): True, + }.items(): + self.assertEqual(sl(inpt), otpt) + +unittest.main() diff --git a/challenge-333/pokgopun/python/ch-2.py b/challenge-333/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..eb7d664d0f --- /dev/null +++ b/challenge-333/pokgopun/python/ch-2.py @@ -0,0 +1,78 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-333/ +""" + +Task 2: Duplicate Zeros + +Submitted by: [44]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers. + + Write a script to duplicate each occurrence of zero, shifting the + remaining elements to the right. The elements beyond the length of the + original array are not written. + +Example 1 + +Input: @ints = (1, 0, 2, 3, 0, 4, 5, 0) +Output: (1, 0, 0, 2, 3, 0, 0, 4) + +Each zero is duplicated. +Elements beyond the original length (like 5 and last 0) are discarded. + +Example 2 + +Input: @ints = (1, 2, 3) +Output: (1, 2, 3) + +No zeros exist, so the array remains unchanged. + +Example 3 + +Input: @ints = (1, 2, 3, 0) +Output: (1, 2, 3, 0) + +Example 4 + +Input: @ints = (0, 0, 1, 2) +Output: (0, 0, 0, 0) + +Example 5 + +Input: @ints = (1, 2, 0, 3, 4) +Output: (1, 2, 0, 0, 3) + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 10th August + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def dz(ints: tuple[int]) -> tuple[int]: + ints = list(ints) + l = len(ints) - 1 + i = 0 + while i < l: + if ints[i] == 0: + ints.insert(i,0) + i += 1 + i += 1 + return tuple(ints[:l+1]) + +import unittest + +class TestDz(unittest.TestCase): + def test(self): + for inpt, otpt in { + (1, 0, 2, 3, 0, 4, 5, 0): (1, 0, 0, 2, 3, 0, 0, 4), + (1, 2, 3): (1, 2, 3), + (1, 2, 3, 0): (1, 2, 3, 0), + (0, 0, 1, 2): (0, 0, 0, 0), + (1, 2, 0, 3, 4): (1, 2, 0, 0, 3), + }.items(): + self.assertEqual(dz(inpt),otpt) + +unittest.main() |
