diff options
| -rw-r--r-- | challenge-337/pokgopun/go/ch-1.go | 86 | ||||
| -rw-r--r-- | challenge-337/pokgopun/go/ch-2.go | 294 | ||||
| -rw-r--r-- | challenge-337/pokgopun/python/ch-1.py | 67 | ||||
| -rw-r--r-- | challenge-337/pokgopun/python/ch-2.py | 273 |
4 files changed, 720 insertions, 0 deletions
diff --git a/challenge-337/pokgopun/go/ch-1.go b/challenge-337/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..352fa976cd --- /dev/null +++ b/challenge-337/pokgopun/go/ch-1.go @@ -0,0 +1,86 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-337/ +/*# + +Task 1: Smaller Than Current + +Submitted by: [40]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of numbers, @num1. + + Write a script to return an array, @num2, where $num2[i] is the count + of all numbers less than or equal to $num1[i]. + +Example 1 + +Input: @num1 = (6, 5, 4, 8) +Output: (2, 1, 0, 3) + +index 0: numbers <= 6 are 5, 4 => 2 +index 1: numbers <= 5 are 4 => 1 +index 2: numbers <= 4, none => 0 +index 3: numbers <= 8 are 6, 5, 4 => 3 + +Example 2 + +Input: @num1 = (7, 7, 7, 7) +Output: (3, 3, 3, 3) + +Example 3 + +Input: @num1 = (5, 4, 3, 2, 1) +Output: (4, 3, 2, 1, 0) + +Example 4 + +Input: @num1 = (-1, 0, 3, -2, 1) +Output: (1, 2, 4, 0, 3) + +Example 5 + +Input: @num1 = (0, 1, 1, 2, 0) +Output: (1, 3, 3, 4, 1) + +Task 2: Odd Matrix +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type ints []int + +func (in ints) process() ints { + l := len(in) + s := make(ints, l) + for i, v := range in { + j := l + for j > 0 { + j-- + if j != i && in[j] <= v { + s[i]++ + } + } + } + return s +} + +func main() { + for _, data := range []struct { + input, output ints + }{ + {ints{6, 5, 4, 8}, ints{2, 1, 0, 3}}, + {ints{7, 7, 7, 7}, ints{3, 3, 3, 3}}, + {ints{5, 4, 3, 2, 1}, ints{4, 3, 2, 1, 0}}, + {ints{-1, 0, 3, -2, 1}, ints{1, 2, 4, 0, 3}}, + {ints{0, 1, 1, 2, 0}, ints{1, 3, 3, 4, 1}}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-337/pokgopun/go/ch-2.go b/challenge-337/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..e30b1a06bb --- /dev/null +++ b/challenge-337/pokgopun/go/ch-2.go @@ -0,0 +1,294 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-337/ +/*# + +Task 2: Odd Matrix + +Submitted by: [41]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given row and col, also a list of positions in the matrix. + + Write a script to perform action on each location (0-indexed) as + provided in the list and find out the total odd valued cells. + + For each location (r, c), do both of the following: +a) Increment by 1 all the cells on row r. +b) Increment by 1 all the cells on column c. + +Example 1 + +Input: $row = 2, $col = 3, @locations = ([0,1],[1,1]) +Output: 6 + +Initial: +[ 0 0 0 ] +[ 0 0 0 ] + +Apply [0,1]: +Increment row 0: +Before After +[ 0 0 0 ] [ 1 1 1 ] +[ 0 0 0 ] [ 0 0 0 ] +Increment col 1: +Before After +[ 1 1 1 ] [ 1 2 1 ] +[ 0 0 0 ] [ 0 1 0 ] + +Apply [1,1]: +Increment row 1: +Before After +[ 1 2 1 ] [ 1 2 1 ] +[ 0 1 0 ] [ 1 2 1 ] +Increment col 1: +Before After +[ 1 2 1 ] [ 1 3 1 ] +[ 1 2 1 ] [ 1 3 1 ] + +Final: +[ 1 3 1 ] +[ 1 3 1 ] + +Example 2 + +Input: $row = 2, $col = 2, @locations = ([1,1],[0,0]) +Output: 0 + +Initial: +[ 0 0 ] +[ 0 0 ] + +Apply [1,1]: +Increment row 1: +Before After +[ 0 0 ] [ 0 0 ] +[ 0 0 ] [ 1 1 ] +Increment col 1: +Before After +[ 0 0 ] [ 0 1 ] +[ 1 1 ] [ 1 2 ] + +Apply [0,0]: +Increment row 0: +Before After +[ 0 1 ] [ 1 2 ] +[ 1 2 ] [ 1 2 ] +Increment col 0: +Before After +[ 1 2 ] [ 2 2 ] +[ 1 2 ] [ 2 2 ] + +Final: +[ 2 2 ] +[ 2 2 ] + +Example 3 + +Input: $row = 3, $col = 3, @locations = ([0,0],[1,2],[2,1]) +Output: 0 + +Initial: +[ 0 0 0 ] +[ 0 0 0 ] +[ 0 0 0 ] + +Apply [0,0]: +Increment row 0: +Before After +[ 0 0 0 ] [ 1 1 1 ] +[ 0 0 0 ] [ 0 0 0 ] +[ 0 0 0 ] [ 0 0 0 ] +Increment col 0: +Before After +[ 1 1 1 ] [ 2 1 1 ] +[ 0 0 0 ] [ 1 0 0 ] +[ 0 0 0 ] [ 1 0 0 ] + +Apply [1,2]: +Increment row 1: +Before After +[ 2 1 1 ] [ 2 1 1 ] +[ 1 0 0 ] [ 2 1 1 ] +[ 1 0 0 ] [ 1 0 0 ] +Increment col 2: +Before After +[ 2 1 1 ] [ 2 1 2 ] +[ 2 1 1 ] [ 2 1 2 ] +[ 1 0 0 ] [ 1 0 1 ] + +Apply [2,1]: +Increment row 2: +Before After +[ 2 1 2 ] [ 2 1 2 ] +[ 2 1 2 ] [ 2 1 2 ] +[ 1 0 1 ] [ 2 1 2 ] +Increment col 1: +Before After +[ 2 1 2 ] [ 2 2 2 ] +[ 2 1 2 ] [ 2 2 2 ] +[ 2 1 2 ] [ 2 2 2 ] + +Final: +[ 2 2 2 ] +[ 2 2 2 ] +[ 2 2 2 ] + +Example 4 + +Input: $row = 1, $col = 5, @locations = ([0,2],[0,4]) +Output: 2 + +Initial: +[ 0 0 0 0 0 ] + +Apply [0,2]: +Increment row 0: +Before After +[ 0 0 0 0 0 ] [ 1 1 1 1 1 ] +Increment col 2: +Before After +[ 1 1 1 1 1 ] [ 1 1 2 1 1 ] + +Apply [0,4]: +Increment row 0: +Before After +[ 1 1 2 1 1 ] [ 2 2 3 2 2 ] +Increment col 4: +Before After +[ 2 2 3 2 2 ] [ 2 2 3 2 3 ] + +Final: +[ 2 2 3 2 3 ] + +Example 5 + +Input: $row = 4, $col = 2, @locations = ([1,0],[3,1],[2,0],[0,1]) +Output: 8 + +Initial: +[ 0 0 ] +[ 0 0 ] +[ 0 0 ] +[ 0 0 ] + +Apply [1,0]: +Increment row 1: +Before After +[ 0 0 ] [ 0 0 ] +[ 0 0 ] [ 1 1 ] +[ 0 0 ] [ 0 0 ] +[ 0 0 ] [ 0 0 ] +Increment col 0: +Before After +[ 0 0 ] [ 1 0 ] +[ 1 1 ] [ 2 1 ] +[ 0 0 ] [ 1 0 ] +[ 0 0 ] [ 1 0 ] + +Apply [3,1]: +Increment row 3: +Before After +[ 1 0 ] [ 1 0 ] +[ 2 1 ] [ 2 1 ] +[ 1 0 ] [ 1 0 ] +[ 1 0 ] [ 2 1 ] +Increment col 1: +Before After +[ 1 0 ] [ 1 1 ] +[ 2 1 ] [ 2 2 ] +[ 1 0 ] [ 1 1 ] +[ 2 1 ] [ 2 2 ] + +Apply [2,0]: +Increment row 2: +Before After +[ 1 1 ] [ 1 1 ] +[ 2 2 ] [ 2 2 ] +[ 1 1 ] [ 2 2 ] +[ 2 2 ] [ 2 2 ] +Increment col 0: +Before After +[ 1 1 ] [ 2 1 ] +[ 2 2 ] [ 3 2 ] +[ 2 2 ] [ 3 2 ] +[ 2 2 ] [ 3 2 ] + +Apply [0,1]: +Increment row 0: +Before After +[ 2 1 ] [ 3 2 ] +[ 3 2 ] [ 3 2 ] +[ 3 2 ] [ 3 2 ] +[ 3 2 ] [ 3 2 ] +Increment col 1: +Before After +[ 3 2 ] [ 3 3 ] +[ 3 2 ] [ 3 3 ] +[ 3 2 ] [ 3 3 ] +[ 3 2 ] [ 3 3 ] + +Final: +[ 3 3 ] +[ 3 3 ] +[ 3 3 ] +[ 3 3 ] + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 7th September + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + + "github.com/google/go-cmp/cmp" +) + +type pos struct { + r, c int +} + +type locations []pos + +type Input struct { + row, col int + locations locations +} + +func (in Input) process() int { + m := make(map[int]int) + for _, v := range in.locations { + m[v.r]++ + m[-1-v.c]++ + } + cnto := 0 + for r := 0; r < in.row; r++ { + for c := 0; c < in.col; c++ { + if (m[r]+m[-1-c])%2 == 1 { + cnto++ + } + } + } + return cnto +} + +func main() { + for _, data := range []struct { + input Input + output int + }{ + {Input{2, 3, locations{pos{0, 1}, pos{1, 1}}}, 6}, + {Input{2, 2, locations{pos{1, 1}, pos{0, 0}}}, 0}, + {Input{3, 3, locations{pos{0, 0}, pos{1, 2}, pos{2, 1}}}, 0}, + {Input{1, 5, locations{pos{0, 2}, pos{0, 4}}}, 2}, + {Input{4, 2, locations{pos{1, 0}, pos{3, 1}, pos{2, 0}, pos{0, 1}}}, 8}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.process(), data.output)) // blank if ok, otherwise show the difference + } +} diff --git a/challenge-337/pokgopun/python/ch-1.py b/challenge-337/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..eb7b906a2d --- /dev/null +++ b/challenge-337/pokgopun/python/ch-1.py @@ -0,0 +1,67 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-337/ +""" + +Task 1: Smaller Than Current + +Submitted by: [40]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of numbers, @num1. + + Write a script to return an array, @num2, where $num2[i] is the count + of all numbers less than or equal to $num1[i]. + +Example 1 + +Input: @num1 = (6, 5, 4, 8) +Output: (2, 1, 0, 3) + +index 0: numbers <= 6 are 5, 4 => 2 +index 1: numbers <= 5 are 4 => 1 +index 2: numbers <= 4, none => 0 +index 3: numbers <= 8 are 6, 5, 4 => 3 + +Example 2 + +Input: @num1 = (7, 7, 7, 7) +Output: (3, 3, 3, 3) + +Example 3 + +Input: @num1 = (5, 4, 3, 2, 1) +Output: (4, 3, 2, 1, 0) + +Example 4 + +Input: @num1 = (-1, 0, 3, -2, 1) +Output: (1, 2, 4, 0, 3) + +Example 5 + +Input: @num1 = (0, 1, 1, 2, 0) +Output: (1, 3, 3, 4, 1) + +Task 2: Odd Matrix +""" +### solution by pokgopun@gmail.com + +def stc(ints: tuple[int]) -> int: + l = len(ints) + return tuple( + sum(1 for i in range(l) if i != j and ints[i] <= ints[j]) for j in range(l) + ) + +import unittest + +class TestStc(unittest.TestCase): + def test(self): + for inpt, otpt in { + (6, 5, 4, 8): (2, 1, 0, 3), + (7, 7, 7, 7): (3, 3, 3, 3), + (5, 4, 3, 2, 1): (4, 3, 2, 1, 0), + (-1, 0, 3, -2, 1): (1, 2, 4, 0, 3), + (0, 1, 1, 2, 0): (1, 3, 3, 4, 1), + }.items(): + self.assertEqual(stc(inpt),otpt) + +unittest.main() diff --git a/challenge-337/pokgopun/python/ch-2.py b/challenge-337/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..1aaef66a6d --- /dev/null +++ b/challenge-337/pokgopun/python/ch-2.py @@ -0,0 +1,273 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-337/ +""" + +Task 2: Odd Matrix + +Submitted by: [41]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given row and col, also a list of positions in the matrix. + + Write a script to perform action on each location (0-indexed) as + provided in the list and find out the total odd valued cells. + + For each location (r, c), do both of the following: +a) Increment by 1 all the cells on row r. +b) Increment by 1 all the cells on column c. + +Example 1 +Input: $row = 2, $col = 3, @locations = ([0,1],[1,1]) +Output: 6 + +Initial: +[ 0 0 0 ] +[ 0 0 0 ] + +Apply [0,1]: +Increment row 0: +Before After +[ 0 0 0 ] [ 1 1 1 ] +[ 0 0 0 ] [ 0 0 0 ] +Increment col 1: +Before After +[ 1 1 1 ] [ 1 2 1 ] +[ 0 0 0 ] [ 0 1 0 ] + +Apply [1,1]: +Increment row 1: +Before After +[ 1 2 1 ] [ 1 2 1 ] +[ 0 1 0 ] [ 1 2 1 ] +Increment col 1: +Before After +[ 1 2 1 ] [ 1 3 1 ] +[ 1 2 1 ] [ 1 3 1 ] + +Final: +[ 1 3 1 ] +[ 1 3 1 ] + +Example 2 +Input: $row = 2, $col = 2, @locations = ([1,1],[0,0]) +Output: 0 + +Initial: +[ 0 0 ] +[ 0 0 ] + +Apply [1,1]: +Increment row 1: +Before After +[ 0 0 ] [ 0 0 ] +[ 0 0 ] [ 1 1 ] +Increment col 1: +Before After +[ 0 0 ] [ 0 1 ] +[ 1 1 ] [ 1 2 ] + +Apply [0,0]: +Increment row 0: +Before After +[ 0 1 ] [ 1 2 ] +[ 1 2 ] [ 1 2 ] +Increment col 0: +Before After +[ 1 2 ] [ 2 2 ] +[ 1 2 ] [ 2 2 ] + +Final: +[ 2 2 ] +[ 2 2 ] + +Example 3 +Input: $row = 3, $col = 3, @locations = ([0,0],[1,2],[2,1]) +Output: 0 + +Initial: +[ 0 0 0 ] +[ 0 0 0 ] +[ 0 0 0 ] + +Apply [0,0]: +Increment row 0: +Before After +[ 0 0 0 ] [ 1 1 1 ] +[ 0 0 0 ] [ 0 0 0 ] +[ 0 0 0 ] [ 0 0 0 ] +Increment col 0: +Before After +[ 1 1 1 ] [ 2 1 1 ] +[ 0 0 0 ] [ 1 0 0 ] +[ 0 0 0 ] [ 1 0 0 ] + +Apply [1,2]: +Increment row 1: +Before After +[ 2 1 1 ] [ 2 1 1 ] +[ 1 0 0 ] [ 2 1 1 ] +[ 1 0 0 ] [ 1 0 0 ] +Increment col 2: +Before After +[ 2 1 1 ] [ 2 1 2 ] +[ 2 1 1 ] [ 2 1 2 ] +[ 1 0 0 ] [ 1 0 1 ] + +Apply [2,1]: +Increment row 2: +Before After +[ 2 1 2 ] [ 2 1 2 ] +[ 2 1 2 ] [ 2 1 2 ] +[ 1 0 1 ] [ 2 1 2 ] +Increment col 1: +Before After +[ 2 1 2 ] [ 2 2 2 ] +[ 2 1 2 ] [ 2 2 2 ] +[ 2 1 2 ] [ 2 2 2 ] + +Final: +[ 2 2 2 ] +[ 2 2 2 ] +[ 2 2 2 ] + +Example 4 +Input: $row = 1, $col = 5, @locations = ([0,2],[0,4]) +Output: 2 + +Initial: +[ 0 0 0 0 0 ] + +Apply [0,2]: +Increment row 0: +Before After +[ 0 0 0 0 0 ] [ 1 1 1 1 1 ] +Increment col 2: +Before After +[ 1 1 1 1 1 ] [ 1 1 2 1 1 ] + +Apply [0,4]: +Increment row 0: +Before After +[ 1 1 2 1 1 ] [ 2 2 3 2 2 ] +Increment col 4: +Before After +[ 2 2 3 2 2 ] [ 2 2 3 2 3 ] + +Final: +[ 2 2 3 2 3 ] + +Example 5 +Input: $row = 4, $col = 2, @locations = ([1,0],[3,1],[2,0],[0,1]) +Output: 8 + +Initial: +[ 0 0 ] +[ 0 0 ] +[ 0 0 ] +[ 0 0 ] + +Apply [1,0]: +Increment row 1: +Before After +[ 0 0 ] [ 0 0 ] +[ 0 0 ] [ 1 1 ] +[ 0 0 ] [ 0 0 ] +[ 0 0 ] [ 0 0 ] +Increment col 0: +Before After +[ 0 0 ] [ 1 0 ] +[ 1 1 ] [ 2 1 ] +[ 0 0 ] [ 1 0 ] +[ 0 0 ] [ 1 0 ] + +Apply [3,1]: +Increment row 3: +Before After +[ 1 0 ] [ 1 0 ] +[ 2 1 ] [ 2 1 ] +[ 1 0 ] [ 1 0 ] +[ 1 0 ] [ 2 1 ] +Increment col 1: +Before After +[ 1 0 ] [ 1 1 ] +[ 2 1 ] [ 2 2 ] +[ 1 0 ] [ 1 1 ] +[ 2 1 ] [ 2 2 ] + +Apply [2,0]: +Increment row 2: +Before After +[ 1 1 ] [ 1 1 ] +[ 2 2 ] [ 2 2 ] +[ 1 1 ] [ 2 2 ] +[ 2 2 ] [ 2 2 ] +Increment col 0: +Before After +[ 1 1 ] [ 2 1 ] +[ 2 2 ] [ 3 2 ] +[ 2 2 ] [ 3 2 ] +[ 2 2 ] [ 3 2 ] + +Apply [0,1]: +Increment row 0: +Before After +[ 2 1 ] [ 3 2 ] +[ 3 2 ] [ 3 2 ] +[ 3 2 ] [ 3 2 ] +[ 3 2 ] [ 3 2 ] +Increment col 1: +Before After +[ 3 2 ] [ 3 3 ] +[ 3 2 ] [ 3 3 ] +[ 3 2 ] [ 3 3 ] +[ 3 2 ] [ 3 3 ] + +Final: +[ 3 3 ] +[ 3 3 ] +[ 3 3 ] +[ 3 3 ] + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 7th September + 2025. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +from dataclasses import dataclass + +@dataclass +class Position: + r: int + c: int + +def om(row: int, col: int, positions: tuple[Position]) -> int: + p2c: dict[int,int] = {} + for p in positions: + p2c[p.r] = p2c.get(p.r,0) + 1 + p2c[-1-p.c] = p2c.get(-1-p.c,0) + 1 + #print(p2c) + cnto = 0 + for r in range(row): + for c in range(col): + if (p2c.get(r,0)+p2c.get(-1-c,0)) % 2 == 1: + cnto += 1 + return cnto + +import unittest + +class TestOm(unittest.TestCase): + def test(self): + for (row, col, locations), otpt in { + (2, 3, ((0,1),(1,1))): 6, + (2, 2, ((1,1),(0,0))): 0, + (3, 3, ((0,0),(1,2),(2,1))): 0, + (1, 5, ((0,2),(0,4))): 2, + (4, 2, ((1,0),(3,1),(2,0),(0,1))): 8, + }.items(): + #print(row, col, locations, otpt) + self.assertEqual(om(row, col, tuple(Position(e[0],e[1]) for e in locations)), otpt) + +unittest.main() |
