diff options
| author | Packy Anderson <packy@cpan.org> | 2023-11-17 00:26:15 -0500 |
|---|---|---|
| committer | Packy Anderson <packy@cpan.org> | 2023-11-17 00:26:15 -0500 |
| commit | f7a9faeb822c20217b1d1c00a008bd247397c3a8 (patch) | |
| tree | 3c107c6e50b27f333127ef8fb3722a32493f7c8b /challenge-243/packy-anderson/python | |
| parent | 1a59d9883fd5f113ea3b6ad4c045dcdc87fcdb83 (diff) | |
| download | perlweeklychallenge-club-f7a9faeb822c20217b1d1c00a008bd247397c3a8.tar.gz perlweeklychallenge-club-f7a9faeb822c20217b1d1c00a008bd247397c3a8.tar.bz2 perlweeklychallenge-club-f7a9faeb822c20217b1d1c00a008bd247397c3a8.zip | |
Challenge 243 solutions by Packy Anderson
* Raku
* Perl
* Python
1 Blog post
Diffstat (limited to 'challenge-243/packy-anderson/python')
| -rwxr-xr-x | challenge-243/packy-anderson/python/ch-1.py | 36 | ||||
| -rwxr-xr-x | challenge-243/packy-anderson/python/ch-2.py | 24 |
2 files changed, 60 insertions, 0 deletions
diff --git a/challenge-243/packy-anderson/python/ch-1.py b/challenge-243/packy-anderson/python/ch-1.py new file mode 100755 index 0000000000..d490ef0bf1 --- /dev/null +++ b/challenge-243/packy-anderson/python/ch-1.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +from collections import UserList + +class ReversePairArray(UserList): + def isReversePair(self, i, j): + return self.data[i] > 2 * self.data[j] + +def findReversePairs(nums): + pairs = [] + rpArray = ReversePairArray(nums) + for i in range(0, len(nums) - 1): + for j in range(i+1, len(nums)): + if rpArray.isReversePair(i, j): + pairs.append([i, j]) + return pairs + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(nums): + print(f'Input: @nums = ({comma_join(nums)})') + pairs = findReversePairs(nums) + print(f'Output: {len(pairs)}') + for pair in pairs: + (i, j) = pair + print(f'({i}, {j}) => ', end='') + print(f'nums[{i}] = {nums[i]}, ', end='') + print(f'nums[{j}] = {nums[j]}, ', end='') + print(f'{nums[i]} > 2 * {nums[j]}') + +print('Example 1:') +solution([1, 3, 2, 3, 1]) + +print('\nExample 2:') +solution([2, 4, 3, 5, 1]) diff --git a/challenge-243/packy-anderson/python/ch-2.py b/challenge-243/packy-anderson/python/ch-2.py new file mode 100755 index 0000000000..8b2172ab3a --- /dev/null +++ b/challenge-243/packy-anderson/python/ch-2.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python + +from math import trunc + +def floorSum(nums): + sum = 0; + for i in range(0, len(nums)): + for j in range(0, len(nums)): + sum += trunc(nums[i] / nums[j]) + return sum + +def comma_join(arr): + return ', '.join(map(lambda i: str(i), arr)) + +def solution(nums): + print(f'Input: @nums = ({comma_join(nums)})') + sum = floorSum(nums) + print(f'Output: {sum}') + +print('Example 1:') +solution([2, 5, 9]) + +print('\nExample 2:') +solution([7, 7, 7, 7, 7, 7, 7]) |
