aboutsummaryrefslogtreecommitdiff
path: root/challenge-263
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-04-05 10:47:54 +0100
committerGitHub <noreply@github.com>2024-04-05 10:47:54 +0100
commit4e7fbe49e63591415597a6bbe2b6b538cc02297d (patch)
treeb69332f4440175bd195e3b7f4d2772809021c05f /challenge-263
parent6ca7d47cfcffda1ad03db11057afe4f419d4b224 (diff)
parentf4a8cee131b9f4241d2e58babf11e1ad5e0ea2e5 (diff)
downloadperlweeklychallenge-club-4e7fbe49e63591415597a6bbe2b6b538cc02297d.tar.gz
perlweeklychallenge-club-4e7fbe49e63591415597a6bbe2b6b538cc02297d.tar.bz2
perlweeklychallenge-club-4e7fbe49e63591415597a6bbe2b6b538cc02297d.zip
Merge pull request #9870 from packy/master
Challenge 263 solutions by Packy Anderson
Diffstat (limited to 'challenge-263')
-rw-r--r--challenge-263/packy-anderson/README.md2
-rw-r--r--challenge-263/packy-anderson/blog.txt1
-rwxr-xr-xchallenge-263/packy-anderson/perl/ch-1.pl41
-rwxr-xr-xchallenge-263/packy-anderson/perl/ch-2.pl46
-rwxr-xr-xchallenge-263/packy-anderson/python/ch-1.py39
-rwxr-xr-xchallenge-263/packy-anderson/python/ch-2.py44
-rwxr-xr-xchallenge-263/packy-anderson/raku/ch-1.raku41
-rwxr-xr-xchallenge-263/packy-anderson/raku/ch-2.raku46
8 files changed, 259 insertions, 1 deletions
diff --git a/challenge-263/packy-anderson/README.md b/challenge-263/packy-anderson/README.md
index b5c4eea6fb..372a505456 100644
--- a/challenge-263/packy-anderson/README.md
+++ b/challenge-263/packy-anderson/README.md
@@ -16,4 +16,4 @@
## Blog Post
-[Counting to the Max!](https://packy.dardan.com/b/Jx)
+[Merge the Target Index Items](https://packy.dardan.com/b/KC)
diff --git a/challenge-263/packy-anderson/blog.txt b/challenge-263/packy-anderson/blog.txt
new file mode 100644
index 0000000000..cdc797043a
--- /dev/null
+++ b/challenge-263/packy-anderson/blog.txt
@@ -0,0 +1 @@
+https://packy.dardan.com/b/KC \ No newline at end of file
diff --git a/challenge-263/packy-anderson/perl/ch-1.pl b/challenge-263/packy-anderson/perl/ch-1.pl
new file mode 100755
index 0000000000..f968896c60
--- /dev/null
+++ b/challenge-263/packy-anderson/perl/ch-1.pl
@@ -0,0 +1,41 @@
+#!/usr/bin/env perl
+use v5.38;
+
+sub targetIndex($k, @ints) {
+ my @sorted = sort @ints;
+ my $explain = 'Sorted array: (' . join(', ', @sorted) . ")\n";
+
+ my @output;
+ foreach my $i (0 .. $#sorted) {
+ next unless $sorted[$i] == $k;
+ push @output, $i;
+ }
+ if (@output == 0) {
+ $explain .= 'No element in the given array matching '
+ . 'the given target.';
+ }
+ else {
+ $explain .= 'Target indices: (' . join(', ', @output)
+ . ') as ';
+ my @explain_indices = map { "\$ints[$_] = $k"} @output;
+ $explain .= join(' and ', @explain_indices);
+ }
+ return $explain, @output;
+}
+
+sub solution($k, $ints) {
+ my $intlist = join(', ', @$ints);
+ say "Input: \@ints = ($intlist), \$k = $k";
+ my ($explain, @output) = targetIndex($k, @$ints);
+ $intlist = join(', ', @output);
+ say "Output: ($intlist)\n\n$explain";
+}
+
+say "Example 1:";
+solution(2, [1, 5, 3, 2, 4, 2]);
+
+say "\nExample 2:";
+solution(6, [1, 2, 4, 3, 5]);
+
+say "\nExample 3:";
+solution(4, [5, 3, 2, 4, 2, 1]); \ No newline at end of file
diff --git a/challenge-263/packy-anderson/perl/ch-2.pl b/challenge-263/packy-anderson/perl/ch-2.pl
new file mode 100755
index 0000000000..a5be15e629
--- /dev/null
+++ b/challenge-263/packy-anderson/perl/ch-2.pl
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl
+use v5.38;
+
+sub mergeItems($items1, $items2) {
+ my %merged;
+ # loop over the items and add item_quantities (element 1)
+ # to the count for each item_id (element 0)
+ foreach my $i (@$items1, @$items2) {
+ $merged{$i->[0]} += $i->[1];
+ }
+ # re-render the hash as a 2D array
+ return [ map { [ $_, $merged{$_} ] } sort keys %merged ];
+}
+
+sub string2D($array) {
+ my @output;
+ foreach my $i (@$array) {
+ push @output, '[' . join(',', @$i) . ']';
+ }
+ return '[ ' . join(', ', @output) . ' ]';
+}
+
+sub solution($items1, $items2) {
+ say 'Input: $items1 = ' . string2D($items1);
+ say ' $items2 = ' . string2D($items2);
+ my $merged = mergeItems($items1, $items2);
+ say 'Output: ' . string2D($merged);
+}
+
+say "Example 1:";
+solution(
+ [ [1,1], [2,1], [3,2] ],
+ [ [2,2], [1,3] ]
+);
+
+say "\nExample 2:";
+solution(
+ [ [1,2], [2,3], [1,3], [3,2] ],
+ [ [3,1], [1,3] ]
+);
+
+say "\nExample 3:";
+solution(
+ [ [1,1], [2,2], [3,3] ],
+ [ [2,3], [2,4] ]
+); \ No newline at end of file
diff --git a/challenge-263/packy-anderson/python/ch-1.py b/challenge-263/packy-anderson/python/ch-1.py
new file mode 100755
index 0000000000..42d597fbb6
--- /dev/null
+++ b/challenge-263/packy-anderson/python/ch-1.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def targetIndex(k, ints):
+ sortedArray = sorted(ints)
+ explain = f'Sorted array: ({comma_join(sortedArray)})\n'
+
+ output = []
+ for i, v in enumerate(sortedArray):
+ if v == k:
+ output.append(i)
+
+ if len(output) == 0:
+ explain += 'No element in the given array matching '
+ explain += 'the given target.'
+ else:
+ explain += f'Target indices: ({comma_join(output)}) as '
+ explain_indices = [ f'$ints[{i}] = {k}' for i in output ]
+ explain += ' and '.join(
+ map(lambda i: str(i), explain_indices)
+ )
+ return explain, output
+
+def solution(k, ints):
+ print(f'Input: @ints = ({comma_join(ints)}), $k = {k}')
+ explain, output = targetIndex(k, ints)
+ print(f'Output: ({comma_join(output)})\n')
+ print(explain)
+
+print('Example 1:')
+solution(2, [1, 5, 3, 2, 4, 2])
+
+print('\nExample 2:')
+solution(6, [1, 2, 4, 3, 5])
+
+print('\nExample 3:')
+solution(4, [5, 3, 2, 4, 2, 1]) \ No newline at end of file
diff --git a/challenge-263/packy-anderson/python/ch-2.py b/challenge-263/packy-anderson/python/ch-2.py
new file mode 100755
index 0000000000..f87cd082a5
--- /dev/null
+++ b/challenge-263/packy-anderson/python/ch-2.py
@@ -0,0 +1,44 @@
+#!/usr/bin/env python
+
+from collections import Counter
+from itertools import chain
+
+def mergeItems(items1, items2):
+ merged = Counter()
+ # loop over the items and add item_quantities (element 1)
+ # to the count for each item_id (element 0)
+ for i in chain(items1, items2):
+ merged[ i[0] ] += i[1]
+
+ # re-render the hash as a 2D array
+ return [ [i, v] for i, v in merged.items() ]
+
+def solution(items1, items2):
+ print(f'Input: $items1 = {items1}')
+ print(f' $items2 = {items2}')
+ print(f'Output: {mergeItems(items1, items2)}')
+
+print('Example 1:')
+solution(
+ [ [1,1], [2,1], [3,2] ],
+ [ [2,2], [1,3] ]
+)
+
+print('\nExample 2:')
+solution(
+ [ [1,2], [2,3], [1,3], [3,2] ],
+ [ [3,1], [1,3] ]
+)
+
+print('\nExample 3:')
+solution(
+ [ [1,1], [2,2], [3,3] ],
+ [ [2,3], [2,4] ]
+)
+
+def comma_join(arr):
+ return ', '.join(map(lambda i: str(i), arr))
+
+def solution(arr):
+ print(f'Input: @arr = ({comma_join(arr)})')
+ print(f'Output: ({comma_join(arr)})')
diff --git a/challenge-263/packy-anderson/raku/ch-1.raku b/challenge-263/packy-anderson/raku/ch-1.raku
new file mode 100755
index 0000000000..6035a61535
--- /dev/null
+++ b/challenge-263/packy-anderson/raku/ch-1.raku
@@ -0,0 +1,41 @@
+#!/usr/bin/env raku
+use v6;
+
+sub targetIndex($k, @ints) {
+ my @sorted = @ints.sort;
+ my $explain = 'Sorted array: (' ~ @sorted.join(', ') ~ ")\n";
+
+ my @output;
+ for @sorted.kv -> $i, $v {
+ next unless $v == $k;
+ @output.push($i);
+ }
+ if (@output == 0) {
+ $explain ~= 'No element in the given array matching '
+ ~ 'the given target.';
+ }
+ else {
+ $explain ~= 'Target indices: (' ~ @output.join(', ')
+ ~ ') as ';
+ my @explain_indices = @output.map({ "\$ints[$_] = $k"});
+ $explain ~= @explain_indices.join(' and ');
+ }
+ return $explain, @output;
+}
+
+sub solution($k, @ints) {
+ my $intlist = @ints.join(', ');
+ say "Input: \@ints = ($intlist), \$k = $k";
+ my ($explain, @output) = targetIndex($k, @ints);
+ $intlist = @output.join(', ');
+ say "Output: ($intlist)\n\n$explain";
+}
+
+say "Example 1:";
+solution(2, [1, 5, 3, 2, 4, 2]);
+
+say "\nExample 2:";
+solution(6, [1, 2, 4, 3, 5]);
+
+say "\nExample 3:";
+solution(4, [5, 3, 2, 4, 2, 1]); \ No newline at end of file
diff --git a/challenge-263/packy-anderson/raku/ch-2.raku b/challenge-263/packy-anderson/raku/ch-2.raku
new file mode 100755
index 0000000000..ee089854cf
--- /dev/null
+++ b/challenge-263/packy-anderson/raku/ch-2.raku
@@ -0,0 +1,46 @@
+#!/usr/bin/env raku
+use v6;
+
+sub mergeItems(@items1, @items2) {
+ my %merged;
+ # loop over the items and add item_quantities (element 1)
+ # to the count for each item_id (element 0)
+ for (slip(@items1), slip(@items2)) -> @i {
+ %merged{@i[0]} += @i[1];
+ }
+ # re-render the hash as a 2D array
+ return %merged.keys.sort.map({ [ $_, %merged{$_} ] });
+}
+
+sub string2D(@array) {
+ my @output;
+ for @array -> @i {
+ @output.push( '[' ~ @i.join(',') ~ ']' );
+ }
+ return '[ ' ~ @output.join(', ') ~ ' ]';
+}
+
+sub solution(@items1, @items2) {
+ say 'Input: $items1 = ' ~ string2D(@items1);
+ say ' $items2 = ' ~ string2D(@items2);
+ my @merged = mergeItems(@items1, @items2);
+ say 'Output: ' ~ string2D(@merged);
+}
+
+say "Example 1:";
+solution(
+ [ [1,1], [2,1], [3,2] ],
+ [ [2,2], [1,3] ]
+);
+
+say "\nExample 2:";
+solution(
+ [ [1,2], [2,3], [1,3], [3,2] ],
+ [ [3,1], [1,3] ]
+);
+
+say "\nExample 3:";
+solution(
+ [ [1,1], [2,2], [3,3] ],
+ [ [2,3], [2,4] ]
+); \ No newline at end of file