diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-04-03 11:39:17 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-03 11:39:17 +0100 |
| commit | 1e4fbe50f80b2ea8665df81d1bfd00d9b6c7c737 (patch) | |
| tree | 072899cb089eb3adc000ed43f784bea6e18c0fc7 | |
| parent | af269b2c83aec2ea31fafe12311be4fcaab4201b (diff) | |
| parent | dfa083bf8b89beb97b5eec5bcc9a16dcbe11a274 (diff) | |
| download | perlweeklychallenge-club-1e4fbe50f80b2ea8665df81d1bfd00d9b6c7c737.tar.gz perlweeklychallenge-club-1e4fbe50f80b2ea8665df81d1bfd00d9b6c7c737.tar.bz2 perlweeklychallenge-club-1e4fbe50f80b2ea8665df81d1bfd00d9b6c7c737.zip | |
Merge pull request #9862 from pokgopun/pwc263
Pwc263
| -rw-r--r-- | challenge-263/pokgopun/go/ch-1.go | 76 | ||||
| -rw-r--r-- | challenge-263/pokgopun/go/ch-2.go | 96 | ||||
| -rw-r--r-- | challenge-263/pokgopun/python/ch-1.py | 59 | ||||
| -rw-r--r-- | challenge-263/pokgopun/python/ch-2.py | 62 |
4 files changed, 293 insertions, 0 deletions
diff --git a/challenge-263/pokgopun/go/ch-1.go b/challenge-263/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..ca1f92accf --- /dev/null +++ b/challenge-263/pokgopun/go/ch-1.go @@ -0,0 +1,76 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-263/ +/*# + +Task 1: Target Index + +Submitted by: [47]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints and a target element $k. + + Write a script to return the list of indices in the sorted array where + the element is same as the given target element. + +Example 1 + +Input: @ints = (1, 5, 3, 2, 4, 2), $k = 2 +Output: (1, 2) + +Sorted array: (1, 2, 2, 3, 4, 5) +Target indices: (1, 2) as $ints[1] = 2 and $ints[2] = 2 + +Example 2 + +Input: @ints = (1, 2, 4, 3, 5), $k = 6 +Output: () + +No element in the given array matching the given target. + +Example 3 + +Input: @ints = (5, 3, 2, 4, 2, 1), $k = 4 +Output: (4) + +Sorted array: (1, 2, 2, 3, 4, 5) +Target index: (4) as $ints[4] = 4 + +Task 2: Merge Items +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "slices" + + "github.com/google/go-cmp/cmp" +) + +type ints []int + +func (is ints) targetIndex(n int) ints { + slices.Sort(is) + res := ints{} + for i, v := range is { + if v == n { + res = append(res, i) + } + } + return res +} + +func main() { + for _, data := range []struct { + input ints + k int + output ints + }{ + {ints{1, 5, 3, 2, 4, 2}, 2, ints{1, 2}}, + {ints{1, 2, 4, 3, 5}, 6, ints{}}, + {ints{5, 3, 2, 4, 2, 1}, 4, ints{4}}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.targetIndex(data.k), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-263/pokgopun/go/ch-2.go b/challenge-263/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..03baedd540 --- /dev/null +++ b/challenge-263/pokgopun/go/ch-2.go @@ -0,0 +1,96 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-263/ +/*# + +Task 2: Merge Items + +Submitted by: [48]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two 2-D array of positive integers, $items1 and $items2 + where element is pair of (item_id, item_quantity). + + Write a script to return the merged items. + +Example 1 + +Input: $items1 = [ [1,1], [2,1], [3,2] ] + $items2 = [ [2,2], [1,3] ] +Output: [ [1,4], [2,3], [3,2] ] + +Item id (1) appears 2 times: [1,1] and [1,3]. Merged item now (1,4) +Item id (2) appears 2 times: [2,1] and [2,2]. Merged item now (2,3) +Item id (3) appears 1 time: [3,2] + +Example 2 + +Input: $items1 = [ [1,2], [2,3], [1,3], [3,2] ] + $items2 = [ [3,1], [1,3] ] +Output: [ [1,8], [2,3], [3,3] ] + +Example 3 + +Input: $items1 = [ [1,1], [2,2], [3,3] ] + $items2 = [ [2,3], [2,4] ] +Output: [ [1,1], [2,9], [3,3] ] + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 7th April 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "sort" + + "github.com/google/go-cmp/cmp" +) + +type Item struct { + Id, Qty int +} + +type Items []Item + +type ItemInv map[int]int + +func (ii ItemInv) addItems(its Items) { + for _, it := range its { + ii[it.Id] += it.Qty + } +} + +func (ii ItemInv) items() Items { + l := len(ii) + its := make(Items, l) + for k, v := range ii { + l-- + its[l] = Item{k, v} + } + sort.Slice(its, func(a, b int) bool { + return its[a].Id < its[b].Id + }) + return its +} + +func main() { + ii := make(ItemInv) + for _, data := range []struct { + items1, items2, merged Items + }{ + {Items{Item{1, 1}, Item{2, 1}, Item{3, 2}}, Items{Item{2, 2}, Item{1, 3}}, Items{Item{1, 4}, Item{2, 3}, Item{3, 2}}}, + {Items{Item{1, 2}, Item{2, 3}, Item{1, 3}, Item{3, 2}}, Items{Item{3, 1}, Item{1, 3}}, Items{Item{1, 8}, Item{2, 3}, Item{3, 3}}}, + {Items{Item{1, 1}, Item{2, 2}, Item{3, 3}}, Items{Item{2, 3}, Item{2, 4}}, Items{Item{1, 1}, Item{2, 9}, Item{3, 3}}}, + } { + ii.addItems(data.items1) + ii.addItems(data.items2) + //fmt.Println(ii.items(), data.merged) + io.WriteString(os.Stdout, cmp.Diff(ii.items(), data.merged)) // blank if ok, otherwise show the difference + clear(ii) + } +} diff --git a/challenge-263/pokgopun/python/ch-1.py b/challenge-263/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..b41204925d --- /dev/null +++ b/challenge-263/pokgopun/python/ch-1.py @@ -0,0 +1,59 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-263/ +""" + +Task 1: Target Index + +Submitted by: [47]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints and a target element $k. + + Write a script to return the list of indices in the sorted array where + the element is same as the given target element. + +Example 1 + +Input: @ints = (1, 5, 3, 2, 4, 2), $k = 2 +Output: (1, 2) + +Sorted array: (1, 2, 2, 3, 4, 5) +Target indices: (1, 2) as $ints[1] = 2 and $ints[2] = 2 + +Example 2 + +Input: @ints = (1, 2, 4, 3, 5), $k = 6 +Output: () + +No element in the given array matching the given target. + +Example 3 + +Input: @ints = (5, 3, 2, 4, 2, 1), $k = 4 +Output: (4) + +Sorted array: (1, 2, 2, 3, 4, 5) +Target index: (4) as $ints[4] = 4 + +Task 2: Merge Items +""" +### solution by pokgopun@gmail.com + +def targetIndex(ints: tuple, k: int): + ints = sorted(ints) + return tuple( + i for i in range(len(ints)) + if ints[i]==k + ) + +import unittest + +class TestTargetIndex(unittest.TestCase): + def test(self): + for (ints, k), ans in { + ((1, 5, 3, 2, 4, 2), 2): (1, 2), + ((1, 2, 4, 3, 5), 6): (), + ((5, 3, 2, 4, 2, 1), 4): (4,), + }.items(): + self.assertEqual(targetIndex(ints, k), ans) + +unittest.main() diff --git a/challenge-263/pokgopun/python/ch-2.py b/challenge-263/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..e4dc48c809 --- /dev/null +++ b/challenge-263/pokgopun/python/ch-2.py @@ -0,0 +1,62 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-263/ +""" + +Task 2: Merge Items + +Submitted by: [48]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given two 2-D array of positive integers, $items1 and $items2 + where element is pair of (item_id, item_quantity). + + Write a script to return the merged items. + +Example 1 + +Input: $items1 = [ [1,1], [2,1], [3,2] ] + $items2 = [ [2,2], [1,3] ] +Output: [ [1,4], [2,3], [3,2] ] + +Item id (1) appears 2 times: [1,1] and [1,3]. Merged item now (1,4) +Item id (2) appears 2 times: [2,1] and [2,2]. Merged item now (2,3) +Item id (3) appears 1 time: [3,2] + +Example 2 + +Input: $items1 = [ [1,2], [2,3], [1,3], [3,2] ] + $items2 = [ [3,1], [1,3] ] +Output: [ [1,8], [2,3], [3,3] ] + +Example 3 + +Input: $items1 = [ [1,1], [2,2], [3,3] ] + $items2 = [ [2,3], [2,4] ] +Output: [ [1,1], [2,9], [3,3] ] + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 7th April 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def mergeItems(items1, items2): + dct = dict() + for items in (items1, items2): + for (k,v) in items: + dct[k] = dct.setdefault(k,0) + v + return tuple(dct.items()) + +import unittest + +class TestMergeItems(unittest.TestCase): + def test(self): + for (items1, items2), merged in { + (( (1,1), (2,1), (3,2) ), ( (2,2), (1,3) )): ( (1,4), (2,3), (3,2) ), + (( (1,2), (2,3), (1,3), (3,2) ), ( (3,1), (1,3) )): ( (1,8), (2,3), (3,3) ), + (( (1,1), (2,2), (3,3) ), ( (2,3), (2,4) )): ( (1,1), (2,9), (3,3) ), + }.items(): + self.assertEqual(mergeItems(items1, items2), merged) + +unittest.main() |
