diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-04-07 10:31:23 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-07 10:31:23 +0100 |
| commit | 241b32ba606303c0e3e2f4ddb6c296ac31db49f8 (patch) | |
| tree | 6d4e1dccbddc46a04fd0640bab72c845b9ef8e2a /challenge-263 | |
| parent | ef7d5994810f67e6f8508d444588f5ee07954f10 (diff) | |
| parent | fa5d52b7ee8746ee0541934b08a4f48fbf13f651 (diff) | |
| download | perlweeklychallenge-club-241b32ba606303c0e3e2f4ddb6c296ac31db49f8.tar.gz perlweeklychallenge-club-241b32ba606303c0e3e2f4ddb6c296ac31db49f8.tar.bz2 perlweeklychallenge-club-241b32ba606303c0e3e2f4ddb6c296ac31db49f8.zip | |
Merge pull request #9878 from MatthiasMuth/muthm-263
Challenge 263 Task 1 and 2 solutions in Perl by Matthias Muth
Diffstat (limited to 'challenge-263')
| -rw-r--r-- | challenge-263/matthias-muth/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-263/matthias-muth/perl/README.md | 97 | ||||
| -rwxr-xr-x | challenge-263/matthias-muth/perl/ch-1.pl | 25 | ||||
| -rwxr-xr-x | challenge-263/matthias-muth/perl/ch-2.pl | 31 | ||||
| -rw-r--r-- | challenge-263/matthias-muth/perl/challenge-263.txt | 62 |
5 files changed, 216 insertions, 0 deletions
diff --git a/challenge-263/matthias-muth/blog.txt b/challenge-263/matthias-muth/blog.txt new file mode 100644 index 0000000000..47d8e8ecbb --- /dev/null +++ b/challenge-263/matthias-muth/blog.txt @@ -0,0 +1 @@ +https://github.com/MatthiasMuth/perlweeklychallenge-club/tree/muthm-263/challenge-263/matthias-muth#readme diff --git a/challenge-263/matthias-muth/perl/README.md b/challenge-263/matthias-muth/perl/README.md new file mode 100644 index 0000000000..eae2b9ed30 --- /dev/null +++ b/challenge-263/matthias-muth/perl/README.md @@ -0,0 +1,97 @@ +# Indexes and Items +**Challenge 263 solutions in Perl by Matthias Muth** + +## Task 1: Target Index + +> You are given an array of integers, @ints and a target element $k.<br/> +> Write a script to return the list of indices in the sorted array where the element is same as the given target element.<br/> +> <br/> +> Example 1<br/> +> Input: @ints = (1, 5, 3, 2, 4, 2), $k = 2<br/> +> Output: (1, 2)<br/> +> Sorted array: (1, 2, 2, 3, 4, 5)<br/> +> Target indices: (1, 2) as $ints[1] = 2 and $k[2] = 2<br/> +> <br/> +> Example 2<br/> +> Input: @ints = (1, 2, 4, 3, 5), $k = 6<br/> +> Output: ()<br/> +> No element in the given array matching the given target.<br/> +> <br/> +> Example 3<br/> +> Input: @ints = (5, 3, 2, 4, 2, 1), $k = 4<br/> +> Output: (4)<br/> +> Sorted array: (1, 2, 2, 3, 4, 5)<br/> +> Target index: (4) as $ints[4] = 4<br/> + +I have chosen the straight-forward solution, +sticking to the instructions very closely: +`sort` the array, and `grep` the result for indexes where the +entry matches `$k`. + +Not really spectacular... + +```perl +use v5.36; + +sub target_index( $ints, $k ) { + my @ints = sort $ints->@*; + return grep $ints[$_] == $k, 0..$#ints; +} +``` + +## Task 2: Merge Items + +> You are given two 2-D array of positive integers, $items1 and $items2 where element is pair of (item_id, item_quantity).<br/> +> Write a script to return the merged items.<br/> +> <br/> +> Example 1<br/> +> Input: $items1 = [ [1,1], [2,1], [3,2] ]<br/> +> $items2 = [ [2,2], [1,3] ]<br/> +> Output: [ [1,4], [2,3], [3,2] ]<br/> +> Item id (1) appears 2 times: [1,1] and [1,3]. Merged item now (1,4)<br/> +> Item id (2) appears 2 times: [2,1] and [2,2]. Merged item now (2,3)<br/> +> Item id (3) appears 1 time: [3,2]<br/> +> <br/> +> Example 2<br/> +> Input: $items1 = [ [1,2], [2,3], [1,3], [3,2] ]<br/> +> $items2 = [ [3,1], [1,3] ]<br/> +> Output: [ [1,8], [2,3], [3,3] ]<br/> +> <br/> +> Example 3<br/> +> Input: $items1 = [ [1,1], [2,2], [3,3] ]<br/> +> $items2 = [ [2,3], [2,4] ]<br/> +> Output: [ [1,1], [2,9], [3,3] ]<br/> + +The first thought is to assign the first set of items to a hash, +flattening the entries in the item list, like in +``` perl + my %counts = map $_->@*, $items1->@*; +``` +But this doesn't work, as this will just overwrite entries like `[1,2]` in +the second example by `[1,3]`, which appears later in the same list, +and not add the entries together. + +So we really have to do an addition of all the entries of the `$items1` and +`$items2` lists in a loop. + +The data structure that is expected as a result +is the same as the input structure: +a reference to an array of ( key, value ) pairs, each represented by a short, +two-entries anonymous array. +So we sort the resulting hash by keys to generate that, +and as we put the result into an anonymous array, +we can just return the resulting reference. + + +```perl +use v5.36; + +sub merge_items( $items1, $items2 ) { + my %counts; + $counts{$_->[0]} += $_->[1] + for $items1->@*, $items2->@*; + return [ map [ $_, $counts{$_} ], sort keys %counts ]; +} +``` + +#### **Thank you for the challenge!** diff --git a/challenge-263/matthias-muth/perl/ch-1.pl b/challenge-263/matthias-muth/perl/ch-1.pl new file mode 100755 index 0000000000..c9da136f64 --- /dev/null +++ b/challenge-263/matthias-muth/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl +# +# The Weekly Challenge - Perl & Raku +# (https://theweeklychallenge.org) +# +# Challenge 263 Task 1: Target Index +# +# Perl solution by Matthias Muth. +# + +use v5.36; + +sub target_index( $ints, $k ) { + my @ints = sort $ints->@*; + return grep $ints[$_] == $k, 0..$#ints; +} + +use Test2::V0 qw( -no_srand ); +is [ target_index( [1, 5, 3, 2, 4, 2], 2 ) ], [ 1, 2 ], + 'Example 1: target_index( [1, 5, 3, 2, 4, 2], 2 ) == (1, 2)'; +is [ target_index( [1, 2, 4, 3, 5], 6 ) ], [ ], + 'Example 2: target_index( [1, 2, 4, 3, 5], 6 ) == ()'; +is [ target_index( [5, 3, 2, 4, 2, 1], 4 ) ], [ 4 ], + 'Example 3: target_index( [5, 3, 2, 4, 2, 1], 4 ) == 4'; +done_testing; diff --git a/challenge-263/matthias-muth/perl/ch-2.pl b/challenge-263/matthias-muth/perl/ch-2.pl new file mode 100755 index 0000000000..d491a25404 --- /dev/null +++ b/challenge-263/matthias-muth/perl/ch-2.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl +# +# The Weekly Challenge - Perl & Raku +# (https://theweeklychallenge.org) +# +# Challenge 263 Task 2: Merge Items +# +# Perl solution by Matthias Muth. +# + +use v5.36; +use Data::Dump qw( pp ); + +sub merge_items( $items1, $items2 ) { + #my %counts; + my %counts = map $_->@*, $items1->@*; + say pp \%counts; + $counts{$_->[0]} += $_->[1] + for $items1->@*, $items2->@*; + return [ map [ $_, $counts{$_} ], sort keys %counts ]; + +} + +use Test2::V0 qw( -no_srand ); +is merge_items( [[1, 1], [2, 1], [3, 2]], [[2, 2], [1, 3]] ), [[1, 4], [2, 3], [3, 2]], + 'Example 1: merge_items( [[1, 1], [2, 1], [3, 2]], [[2, 2], [1, 3]] ) == [[1, 4], [2, 3], [3, 2]]'; +is merge_items( [[1, 2], [2, 3], [1, 3], [3, 2]], [[3, 1], [1, 3]] ), [[1, 8], [2, 3], [3, 3]], + 'Example 2: merge_items( [[1, 2], [2, 3], [1, 3], [3, 2]], [[3, 1], [1, 3]] ) == [[1, 8], [2, 3], [3, 3]]'; +is merge_items( [[1, 1], [2, 2], [3, 3]], [[2, 3], [2, 4]] ), [[1, 1], [2, 9], [3, 3]], + 'Example 3: merge_items( [[1, 1], [2, 2], [3, 3]], [[2, 3], [2, 4]] ) == [[1, 1], [2, 9], [3, 3]]'; +done_testing; diff --git a/challenge-263/matthias-muth/perl/challenge-263.txt b/challenge-263/matthias-muth/perl/challenge-263.txt new file mode 100644 index 0000000000..a80967b7c6 --- /dev/null +++ b/challenge-263/matthias-muth/perl/challenge-263.txt @@ -0,0 +1,62 @@ +The Weekly Challenge - 263 +Monday, Apr 1, 2024 + + +Task 1: Target Index +Submitted by: Mohammad Sajid Anwar + +You are given an array of integers, @ints and a target element $k. +Write a script to return the list of indices in the sorted array where the element is same as the given target element. +Example 1 + +Input: @ints = (1, 5, 3, 2, 4, 2), $k = 2 +Output: (1, 2) + +Sorted array: (1, 2, 2, 3, 4, 5) +Target indices: (1, 2) as $ints[1] = 2 and $ints[2] = 2 + +Example 2 + +Input: @ints = (1, 2, 4, 3, 5), $k = 6 +Output: () + +No element in the given array matching the given target. + +Example 3 + +Input: @ints = (5, 3, 2, 4, 2, 1), $k = 4 +Output: (4) + +Sorted array: (1, 2, 2, 3, 4, 5) +Target index: (4) as $ints[4] = 4 + + +Task 2: Merge Items +Submitted by: Mohammad Sajid Anwar + +You are given two 2-D array of positive integers, $items1 and $items2 where element is pair of (item_id, item_quantity). +Write a script to return the merged items. +Example 1 + +Input: $items1 = [ [1,1], [2,1], [3,2] ] + $items2 = [ [2,2], [1,3] ] +Output: [ [1,4], [2,3], [3,2] ] + +Item id (1) appears 2 times: [1,1] and [1,3]. Merged item now (1,4) +Item id (2) appears 2 times: [2,1] and [2,2]. Merged item now (2,3) +Item id (3) appears 1 time: [3,2] + +Example 2 + +Input: $items1 = [ [1,2], [2,3], [1,3], [3,2] ] + $items2 = [ [3,1], [1,3] ] +Output: [ [1,8], [2,3], [3,3] ] + +Example 3 + +Input: $items1 = [ [1,1], [2,2], [3,3] ] + $items2 = [ [2,3], [2,4] ] +Output: [ [1,1], [2,9], [3,3] ] + + +Last date to submit the solution 23:59 (UK Time) Sunday 7th April 2024. |
