aboutsummaryrefslogtreecommitdiff
path: root/challenge-243
diff options
context:
space:
mode:
authorPacky Anderson <packy@cpan.org>2023-11-17 00:26:15 -0500
committerPacky Anderson <packy@cpan.org>2023-11-17 00:26:15 -0500
commitf7a9faeb822c20217b1d1c00a008bd247397c3a8 (patch)
tree3c107c6e50b27f333127ef8fb3722a32493f7c8b /challenge-243
parent1a59d9883fd5f113ea3b6ad4c045dcdc87fcdb83 (diff)
downloadperlweeklychallenge-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')
-rw-r--r--challenge-243/packy-anderson/README.md58
-rw-r--r--challenge-243/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-243/packy-anderson/perl/ch-1.pl37
-rwxr-xr-xchallenge-243/packy-anderson/perl/ch-2.pl26
-rwxr-xr-xchallenge-243/packy-anderson/python/ch-1.py36
-rwxr-xr-xchallenge-243/packy-anderson/python/ch-2.py24
-rwxr-xr-xchallenge-243/packy-anderson/raku/ch-1.raku37
-rwxr-xr-xchallenge-243/packy-anderson/raku/ch-2.raku24
8 files changed, 214 insertions, 29 deletions
diff --git a/challenge-243/packy-anderson/README.md b/challenge-243/packy-anderson/README.md
index 1789b5db95..ff23846b2d 100644
--- a/challenge-243/packy-anderson/README.md
+++ b/challenge-243/packy-anderson/README.md
@@ -8,19 +8,19 @@ Sample output
```
$ raku/ch-1.raku
Example 1:
-Input: @arr1 = (1, 2, 3)
- @arr2 = (2, 4, 6)
-Output: ([1, 3], [4, 6])
+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
-(1, 2, 3) has 2 members (1, 3) missing from the array (2, 4, 6)
-(2, 4, 6) has 2 members (4, 6) missing from the array (1, 2, 3)
Example 2:
-Input: @arr1 = (1, 2, 3, 3)
- @arr2 = (1, 1, 2, 2)
-Output: ([3])
+Input: @nums = (2, 4, 3, 5, 1)
+Output: 3
-(1, 2, 3, 3) has 2 members (3, 3) missing from the array (1, 1, 2, 2)
-(1, 1, 2, 2) has 0 members missing from the array (1, 2, 3, 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](raku/ch-2.raku)
@@ -29,12 +29,12 @@ Sample output
```
$ raku/ch-2.raku
Example 1:
-Input: @matrix = ([1, 1, 0], [1, 0, 1], [0, 0, 0])
-Output: ([1, 0, 0], [0, 1, 0], [1, 1, 1])
+Input: @nums = (2, 5, 9)
+Output: 10
Example 2:
-Input: @matrix = ([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0])
-Output: ([1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0])
+Input: @nums = (7, 7, 7, 7, 7, 7, 7)
+Output: 49
```
## Perl
@@ -45,19 +45,19 @@ Sample output
```
$ perl/ch-1.pl
Example 1:
-Input: @arr1 = (1, 2, 3)
- @arr2 = (2, 4, 6)
-Output: ([1, 3], [4, 6])
+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
-(1, 2, 3) has 2 members (1, 3) missing from the array (2, 4, 6)
-(2, 4, 6) has 2 members (4, 6) missing from the array (1, 2, 3)
Example 2:
-Input: @arr1 = (1, 2, 3, 3)
- @arr2 = (1, 1, 2, 2)
-Output: ([3])
+Input: @nums = (2, 4, 3, 5, 1)
+Output: 3
-(1, 2, 3, 3) has 2 members (3, 3) missing from the array (1, 1, 2, 2)
-(1, 1, 2, 2) has 0 members missing from the array (1, 2, 3, 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](perl/ch-2.pl)
@@ -66,12 +66,12 @@ Sample output
```
$ perl/ch-2.pl
Example 1:
-Input: @matrix = ([1, 1, 0], [1, 0, 1], [0, 0, 0])
-Output: ([1, 0, 0], [0, 1, 0], [1, 1, 1])
+Input: @nums = (2, 5, 9)
+Output: 10
Example 2:
-Input: @matrix = ([1, 1, 0, 0], [1, 0, 0, 1], [0, 1, 1, 1], [1, 0, 1, 0])
-Output: ([1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0])
+Input: @nums = (7, 7, 7, 7, 7, 7, 7)
+Output: 49
```
## Guest Language: Python
@@ -80,4 +80,4 @@ Output: ([1, 1, 0, 0], [0, 1, 1, 0], [0, 0, 0, 1], [1, 0, 1, 0])
## Blog Post
-[Perl Weekly Challenge: Flip the Missing Matrix Members](https://packy.dardan.com/2023/11/05/perl-weekly-challenge-flip-the-missing-matrix-members/)
+[Perl Weekly Challenge: Three of a Reverse Sum Pair](https://packy.dardan.com/b/E5)
diff --git a/challenge-243/packy-anderson/blog.txt b/challenge-243/packy-anderson/blog.txt
new file mode 100644
index 0000000000..f68adc0ae1
--- /dev/null
+++ b/challenge-243/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/b/E5 \ No newline at end of file
diff --git a/challenge-243/packy-anderson/perl/ch-1.pl b/challenge-243/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..b341b6feca
--- /dev/null
+++ b/challenge-243/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,37 @@
+#!/usr/bin/env perl
+use v5.38;
+
+sub isReversePair {
+ my ($arr, $i, $j) = @_;
+ return $arr->[$i] > 2 * $arr->[$j];
+}
+
+sub findReversePairs {
+ my @arr = @_;
+ my @pairs;
+ foreach my $i ( 0 .. $#arr - 1) {
+ foreach my $j ( $i+1 .. $#arr) {
+ push @pairs, [$i, $j] if isReversePair(\@arr, $i, $j);
+ }
+ }
+ return @pairs;
+}
+
+sub solution {
+ my @nums = @_;
+ say 'Input: @nums = (' . join(', ', @nums) . ')';
+ my @pairs = findReversePairs(@nums);
+ say 'Output: ' . scalar(@pairs) . "\n";
+ for my $pair ( @pairs ) {
+ my ($i, $j) = @$pair;
+ print "($i, $j) => ";
+ print "nums[$i] = $nums[$i], nums[$j] = $nums[$j], ";
+ say "$nums[$i] > 2 * $nums[$j]";
+ }
+}
+
+say "Example 1:";
+solution(1, 3, 2, 3, 1);
+
+say "\nExample 2:";
+solution(2, 4, 3, 5, 1);
diff --git a/challenge-243/packy-anderson/perl/ch-2.pl b/challenge-243/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..b15a314f07
--- /dev/null
+++ b/challenge-243/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,26 @@
+#!/usr/bin/env perl
+use v5.38;
+
+sub floorSum {
+ my @arr = @_;
+ my $sum = 0;
+ foreach my $i (0 .. $#arr) {
+ foreach my $j (0 .. $#arr) {
+ $sum += int($arr[$i] / $arr[$j]);
+ }
+ }
+ return $sum;
+}
+
+sub solution {
+ my @nums = @_;
+ say 'Input: @nums = (' . join(', ', @nums) . ')';
+ my $sum = floorSum(@nums);
+ say 'Output: ' . $sum;
+}
+
+say "Example 1:";
+solution(2, 5, 9);
+
+say "\nExample 2:";
+solution(7, 7, 7, 7, 7, 7, 7); \ No newline at end of file
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])
diff --git a/challenge-243/packy-anderson/raku/ch-1.raku b/challenge-243/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..06ebae09e3
--- /dev/null
+++ b/challenge-243/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,37 @@
+#!/usr/bin/env raku
+use v6;
+
+class ReversePairArray is Array {
+ method isReversePair($i, $j) {
+ return self[$i] > 2 * self[$j];
+ }
+}
+
+sub findReversePairs(@arr) {
+ my @pairs;
+ my @rpArray := ReversePairArray.new(@arr);
+ for 0 .. @rpArray.elems - 2 -> $i {
+ for $i+1 .. @rpArray.elems - 1 -> $j {
+ @pairs.push([$i, $j]) if @rpArray.isReversePair($i, $j);
+ }
+ }
+ return @pairs;
+}
+
+sub solution(*@nums) {
+ say 'Input: @nums = (' ~ @nums.join(', ') ~ ')';
+ my @pairs = findReversePairs(@nums);
+ say 'Output: ' ~ @pairs.elems ~ "\n";
+ for @pairs -> @pair {
+ my ($i, $j) = @pair;
+ print "($i, $j) => ";
+ print "nums[$i] = @nums[$i], nums[$j] = @nums[$j], ";
+ say "@nums[$i] > 2 * @nums[$j]";
+ }
+}
+
+say "Example 1:";
+solution(1, 3, 2, 3, 1);
+
+say "\nExample 2:";
+solution(2, 4, 3, 5, 1);
diff --git a/challenge-243/packy-anderson/raku/ch-2.raku b/challenge-243/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..6aa80ebe1c
--- /dev/null
+++ b/challenge-243/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,24 @@
+#!/usr/bin/env raku
+use v6;
+
+sub floorSum(@arr) {
+ my $sum = 0;
+ for 0 .. @arr.elems - 1 -> $i {
+ for 0 .. @arr.elems - 1 -> $j {
+ $sum += (@arr[$i] / @arr[$j]).truncate;
+ }
+ }
+ return $sum;
+}
+
+sub solution(*@nums) {
+ say 'Input: @nums = (' ~ @nums.join(', ') ~ ')';
+ my $sum = floorSum(@nums);
+ say 'Output: ' ~ $sum;
+}
+
+say "Example 1:";
+solution(2, 5, 9);
+
+say "\nExample 2:";
+solution(7, 7, 7, 7, 7, 7, 7); \ No newline at end of file