diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2023-11-13 17:25:48 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-13 17:25:48 +0000 |
| commit | 5e12004fbc10352e89f7100f88e61a4149be920b (patch) | |
| tree | 6eac64344769324befa50b084db140ceabe2f9b0 | |
| parent | 035cf02be5dd61dac43bb76e33db07067c92f018 (diff) | |
| parent | 299bb6dc4c3a77afc72f5a953ab3d4d0e183df0a (diff) | |
| download | perlweeklychallenge-club-5e12004fbc10352e89f7100f88e61a4149be920b.tar.gz perlweeklychallenge-club-5e12004fbc10352e89f7100f88e61a4149be920b.tar.bz2 perlweeklychallenge-club-5e12004fbc10352e89f7100f88e61a4149be920b.zip | |
Merge pull request #9055 from pokgopun/pwc243
pwc243 solution
| -rw-r--r-- | challenge-243/pokgopun/go/ch-1.go | 66 | ||||
| -rw-r--r-- | challenge-243/pokgopun/go/ch-2.go | 70 | ||||
| -rw-r--r-- | challenge-243/pokgopun/python/ch-1.py | 52 | ||||
| -rw-r--r-- | challenge-243/pokgopun/python/ch-2.py | 57 |
4 files changed, 245 insertions, 0 deletions
diff --git a/challenge-243/pokgopun/go/ch-1.go b/challenge-243/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..3ce659dddc --- /dev/null +++ b/challenge-243/pokgopun/go/ch-1.go @@ -0,0 +1,66 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-243/ +/*# + +Task 1: Reverse Pairs + +Submitted by: [50]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of integers. + + Write a script to return the number of reverse pairs in the given + array. + + A reverse pair is a pair (i, j) where: a) 0 <= i < j < nums.length and + b) nums[i] > 2 * nums[j]. + +Example 1 + +Input: @nums = (1, 3, 2, 3, 1) +Output: 2 + +(1, 4) => nums[1] = 3, nums[4] = 1, 3 > 2 * 1 +(3, 4) => nums[3] = 3, nums[4] = 1, 3 > 2 * 1 + +Example 2 + +Input: @nums = (2, 4, 3, 5, 1) +Output: 3 + +(1, 4) => nums[1] = 4, nums[4] = 1, 4 > 2 * 1 +(2, 4) => nums[2] = 3, nums[4] = 1, 3 > 2 * 1 +(3, 4) => nums[3] = 5, nums[4] = 1, 5 > 2 * 1 + +Task 2: Floor Sum +#*/ +//# solution by pokgopun@gmail.com + +package main + +import "fmt" + +type nums []int + +func (n nums) rpCount() int { + c, l := 0, len(n) + for i := 0; i < l-1; i++ { + for j := i + 1; j < l; j++ { + if n[i] > 2*n[j] { + c++ + } + } + } + return c +} + +func main() { + for _, data := range []struct { + input nums + output int + }{ + {nums{1, 3, 2, 3, 1}, 2}, + {nums{2, 4, 3, 5, 1}, 3}, + } { + fmt.Println(data.input.rpCount() == data.output) + } +} diff --git a/challenge-243/pokgopun/go/ch-2.go b/challenge-243/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..a9458aec9f --- /dev/null +++ b/challenge-243/pokgopun/go/ch-2.go @@ -0,0 +1,70 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-243/ +/*# + +Task 2: Floor Sum + +Submitted by: [51]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of positive integers (>=1). + + Write a script to return the sum of floor(nums[i] / nums[j]) where 0 <= + i,j < nums.length. The floor() function returns the integer part of the + division. + +Example 1 + +Input: @nums = (2, 5, 9) +Output: 10 + +floor(2 / 5) = 0 +floor(2 / 9) = 0 +floor(5 / 9) = 0 +floor(2 / 2) = 1 +floor(5 / 5) = 1 +floor(9 / 9) = 1 +floor(5 / 2) = 2 +floor(9 / 2) = 4 +floor(9 / 5) = 1 + +Example 2 + +Input: @nums = (7, 7, 7, 7, 7, 7, 7) +Output: 49 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 19th November + 2023. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import "fmt" + +type nums []int + +func (n nums) floorSum() int { + c, l := 0, len(n) + for i := 0; i < l; i++ { + for j := 0; j < l; j++ { + c += n[i] / n[j] + } + } + return c +} + +func main() { + for _, data := range []struct { + input nums + output int + }{ + {nums{2, 5, 9}, 10}, + {nums{7, 7, 7, 7, 7, 7, 7}, 49}, + } { + fmt.Println(data.input.floorSum() == data.output) + } +} diff --git a/challenge-243/pokgopun/python/ch-1.py b/challenge-243/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..5caf040c6d --- /dev/null +++ b/challenge-243/pokgopun/python/ch-1.py @@ -0,0 +1,52 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-243/ +""" + +Task 1: Reverse Pairs + +Submitted by: [50]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of integers. + + Write a script to return the number of reverse pairs in the given + array. + + A reverse pair is a pair (i, j) where: a) 0 <= i < j < nums.length and + b) nums[i] > 2 * nums[j]. + +Example 1 + +Input: @nums = (1, 3, 2, 3, 1) +Output: 2 + +(1, 4) => nums[1] = 3, nums[4] = 1, 3 > 2 * 1 +(3, 4) => nums[3] = 3, nums[4] = 1, 3 > 2 * 1 + +Example 2 + +Input: @nums = (2, 4, 3, 5, 1) +Output: 3 + +(1, 4) => nums[1] = 4, nums[4] = 1, 4 > 2 * 1 +(2, 4) => nums[2] = 3, nums[4] = 1, 3 > 2 * 1 +(3, 4) => nums[3] = 5, nums[4] = 1, 5 > 2 * 1 + +Task 2: Floor Sum +""" +### solution by pokgopun@gmail.com + +def rpCount(nums: tuple): + c, l = 0, len(nums) + for i in range(l-1): + for j in range(i,l): + if nums[i] > 2 * nums[j]: c += 1 + return c + +for inpt,otpt in { + (1, 3, 2, 3, 1):2, + (2, 4, 3, 5, 1):3, + }.items(): + print(rpCount(inpt)==otpt) + + + diff --git a/challenge-243/pokgopun/python/ch-2.py b/challenge-243/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..d45f06e663 --- /dev/null +++ b/challenge-243/pokgopun/python/ch-2.py @@ -0,0 +1,57 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-243/ +""" + +Task 2: Floor Sum + +Submitted by: [51]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of positive integers (>=1). + + Write a script to return the sum of floor(nums[i] / nums[j]) where 0 <= + i,j < nums.length. The floor() function returns the integer part of the + division. + +Example 1 + +Input: @nums = (2, 5, 9) +Output: 10 + +floor(2 / 5) = 0 +floor(2 / 9) = 0 +floor(5 / 9) = 0 +floor(2 / 2) = 1 +floor(5 / 5) = 1 +floor(9 / 9) = 1 +floor(5 / 2) = 2 +floor(9 / 2) = 4 +floor(9 / 5) = 1 + +Example 2 + +Input: @nums = (7, 7, 7, 7, 7, 7, 7) +Output: 49 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 19th November + 2023. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +def floorSum(tup: tuple): + c, l = 0, len(tup) + for i in range(l): + for j in range(l): + c += tup[j]//tup[i] + return c + +for inpt,otpt in { + (2, 5, 9): 10, + (7, 7, 7, 7, 7, 7, 7): 49, + }.items(): + print(floorSum(inpt)==otpt) + + |
