diff options
| author | Michael Manring <michael@manring> | 2023-11-21 12:58:55 +1100 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2023-11-21 12:58:55 +1100 |
| commit | 30d43f2cefbf5fbb8afe85d54d8be2ee1bae41e6 (patch) | |
| tree | 6a1eee3f8cb04ed6af1d5fed2a3c49a4392cea11 | |
| parent | d3c171ecb6715a7bd21a52fab583c4171605f5c0 (diff) | |
| download | perlweeklychallenge-club-30d43f2cefbf5fbb8afe85d54d8be2ee1bae41e6.tar.gz perlweeklychallenge-club-30d43f2cefbf5fbb8afe85d54d8be2ee1bae41e6.tar.bz2 perlweeklychallenge-club-30d43f2cefbf5fbb8afe85d54d8be2ee1bae41e6.zip | |
pwc244 solution in python
| -rw-r--r-- | challenge-244/pokgopun/python/ch-1.py | 49 | ||||
| -rw-r--r-- | challenge-244/pokgopun/python/ch-2.py | 54 |
2 files changed, 103 insertions, 0 deletions
diff --git a/challenge-244/pokgopun/python/ch-1.py b/challenge-244/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..cbf7c69b43 --- /dev/null +++ b/challenge-244/pokgopun/python/ch-1.py @@ -0,0 +1,49 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-244/ +""" + +Task 1: Count Smaller + +Submitted by: [45]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of integers. + + Write a script to calculate the number of integers smaller than the + integer at each index. + +Example 1 + +Input: @int = (8, 1, 2, 2, 3) +Output: (4, 0, 1, 1, 3) + +For index = 0, count of elements less 8 is 4. +For index = 1, count of elements less 1 is 0. +For index = 2, count of elements less 2 is 1. +For index = 3, count of elements less 2 is 1. +For index = 4, count of elements less 3 is 3. + +Example 2 + +Input: @int = (6, 5, 4, 8) +Output: (2, 1, 0, 3) + +Example 3 + +Input: @int = (2, 2, 2) +Output: (0, 0, 0) + +Task 2: Group Hero +""" +### solution by pokgopun@gmail.com + +def cltiConv(tup: tuple): + return tuple( + sum(1 if x < i else 0 for x in tup) for i in tup + ) + +for inpt,otpt in { + (8, 1, 2, 2, 3):(4, 0, 1, 1, 3), + (6, 5, 4, 8):(2, 1, 0, 3), + (2, 2, 2): (0, 0, 0), + }.items(): + print(otpt==cltiConv(inpt)) diff --git a/challenge-244/pokgopun/python/ch-2.py b/challenge-244/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..75387f38d5 --- /dev/null +++ b/challenge-244/pokgopun/python/ch-2.py @@ -0,0 +1,54 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-244/ +""" + +Task 2: Group Hero + +Submitted by: [46]Mohammad S Anwar + __________________________________________________________________ + + You are given an array of integers representing the strength. + + Write a script to return the sum of the powers of all possible + combinations; power is defined as the square of the largest number in a + sequence, multiplied by the smallest. + +Example 1 + +Input: @nums = (2, 1, 4) +Output: 141 + +Group 1: (2) => square(max(2)) * min(2) => 4 * 2 => 8 +Group 2: (1) => square(max(1)) * min(1) => 1 * 1 => 1 +Group 3: (4) => square(max(4)) * min(4) => 16 * 4 => 64 +Group 4: (2,1) => square(max(2,1)) * min(2,1) => 4 * 1 => 4 +Group 5: (2,4) => square(max(2,4)) * min(2,4) => 16 * 2 => 32 +Group 6: (1,4) => square(max(1,4)) * min(1,4) => 16 * 1 => 16 +Group 7: (2,1,4) => square(max(2,1,4)) * min(2,1,4) => 16 * 1 => 16 + +Sum: 8 + 1 + 64 + 4 + 32 + 16 + 16 => 141 + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 26th November + 2023. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +from itertools import combinations + +def sumOfPower(tup: tuple): + return sum( + sum( + max(x)**2 * min(x) for x in combinations(tup,n) + ) for n in range(1,len(tup)+1) + ) + +for inpt,otpt in { + (2, 1, 4): 141, + }.items(): + print(otpt==sumOfPower(inpt)) + + + |
