diff options
| author | Michael Manring <michael@manring> | 2023-11-13 23:28:37 +1100 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2023-11-13 23:34:05 +1100 |
| commit | ebb103d257a18a7e796f121350be487a26de636f (patch) | |
| tree | 79d220ece2a467ffc4685d9857599073eda5e81e /challenge-243 | |
| parent | aeed5ae2bdafdcf14de24d172392278b8ba0b44f (diff) | |
| download | perlweeklychallenge-club-ebb103d257a18a7e796f121350be487a26de636f.tar.gz perlweeklychallenge-club-ebb103d257a18a7e796f121350be487a26de636f.tar.bz2 perlweeklychallenge-club-ebb103d257a18a7e796f121350be487a26de636f.zip | |
pwc243 solution in python
Diffstat (limited to 'challenge-243')
| -rw-r--r-- | challenge-243/pokgopun/python/ch-1.py | 52 | ||||
| -rw-r--r-- | challenge-243/pokgopun/python/ch-2.py | 57 |
2 files changed, 109 insertions, 0 deletions
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) + + |
