diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-04-03 11:40:56 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-03 11:40:56 +0100 |
| commit | 55ae0dbd2e6b2fcae95cf853e49d82ce1c5ce4d7 (patch) | |
| tree | dba2e5f44c19c9416efb23a9f004bd5c9bfbd2b6 | |
| parent | 58fc984d573b70b8de35a667f3b0e0f64ff6531e (diff) | |
| parent | 29851622f71add58e1b342aad2bb06dbb6436c74 (diff) | |
| download | perlweeklychallenge-club-55ae0dbd2e6b2fcae95cf853e49d82ce1c5ce4d7.tar.gz perlweeklychallenge-club-55ae0dbd2e6b2fcae95cf853e49d82ce1c5ce4d7.tar.bz2 perlweeklychallenge-club-55ae0dbd2e6b2fcae95cf853e49d82ce1c5ce4d7.zip | |
Merge pull request #9864 from mattneleigh/pwc263
new file: challenge-263/mattneleigh/perl/ch-1.pl
| -rwxr-xr-x | challenge-263/mattneleigh/perl/ch-1.pl | 90 | ||||
| -rwxr-xr-x | challenge-263/mattneleigh/perl/ch-2.pl | 132 |
2 files changed, 222 insertions, 0 deletions
diff --git a/challenge-263/mattneleigh/perl/ch-1.pl b/challenge-263/mattneleigh/perl/ch-1.pl new file mode 100755 index 0000000000..f449edc3ff --- /dev/null +++ b/challenge-263/mattneleigh/perl/ch-1.pl @@ -0,0 +1,90 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @data_sets = ( + [ + [ 1, 5, 3, 2, 4, 2 ], + 2 + ], + [ + [ 1, 2, 4, 3, 5 ], + 6 + ], + [ + [ 5, 3, 2, 4, 2, 1 ], + 4 + ] +); + +print("\n"); +foreach my $data_set (@data_sets){ + printf( + "Input: \@ints = (%s), \$k = %d\nOutput: (%s)\n\n", + join(", ", @{$data_set->[0]}), + $data_set->[1], + join(", ", find_matching_sorted_indices($data_set)) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Determine at which indices in a sorted array a given value is found; the +# array need not be sorted when presented for analysis +# Takes one argument: +# * A ref to a data structure that contains the array and the target value +# (e.g. +# [ +# # Array to examine +# [ 1, 5, 3, 2, 4, 2 ], +# +# # Target value +# 2 +# ] +# ) +# Returns: +# * A list of locations at which the target value appeared in a sorted version +# of the array (e.g. ( 1, 2 ) ). If the target value is not found, an empty +# list is returned. +################################################################################ +sub find_matching_sorted_indices{ + + # Start indices at -1 since we need to + # increment before returning the value + # later + my $i = -1; + + return( + map( + { + # Pre-increment + $i++; + + # Evaluate as the index if the value + # matches our target, or the empty + # list if it doesn't + $ARG[0][1] == $_ ? + $i + : + (); + } + sort(@{$ARG[0][0]}) + ) + ); + +} + + + diff --git a/challenge-263/mattneleigh/perl/ch-2.pl b/challenge-263/mattneleigh/perl/ch-2.pl new file mode 100755 index 0000000000..b238e58655 --- /dev/null +++ b/challenge-263/mattneleigh/perl/ch-2.pl @@ -0,0 +1,132 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use English; + +################################################################################ +# Begin main execution +################################################################################ + +my @item_sets = ( + [ + [ [1, 1], [2, 1], [3, 2] ], + [ [2, 2], [1, 3] ] + ], + [ + [ [1, 2], [2, 3], [1, 3], [3, 2] ], + [ [3, 1], [1, 3] ] + ], + [ + [ [1, 1], [2, 2], [3, 3] ], + [ [2, 3], [2, 4] ] + ] +); + +print("\n"); +foreach my $item_set (@item_sets){ + printf( + "Input: \$items1 = [ %s ]\n \$items2 = [ %s ]\n" + . + "Output: [ %s ]\n\n", + item_list_to_string(@{$item_set->[0]}), + item_list_to_string(@{$item_set->[1]}), + item_list_to_string(merge_item_totals($item_set)) + ); +} + +exit(0); +################################################################################ +# End main execution; subroutines follow +################################################################################ + + + +################################################################################ +# Given two or more lists of pairs of item ID numbers and quantities, produce +# a merged list of all items with combined totals +# Takes one argument: +# * A ref to a data structure containing two or more lists of item ID numbers +# and quantities; note that item IDs need not be sorted or unique in any list +# (e.g. +# [ +# [ [1, 2], [2, 3], [1, 3], [3, 2] ], +# [ [3, 1], [1, 3] ] +# ] +# ) +# Returns: +# * A list of unique item IDs and combined totals from both lists, sorted by +# item ID number (e.g. +# ( [ 1, 8 ], [ 2, 3 ], [ 3, 3 ] ) +# ) +################################################################################ +sub merge_item_totals{ + + my %items; + + # Examine every given list + foreach my $list (@{$ARG[0]}){ + # Examine every item in the list + foreach my $item (@{$list}){ + # Check if this ID exists in the table + if($items{$item->[0]}){ + # Have this ID already- add to its total + $items{$item->[0]}[1] += $item->[1]; + } else{ + # Don't have this ID yet- store a copy of + # the item + $items{$item->[0]} = [ @{$item} ]; + } + } + } + + # Build/return a list of the new item + # totals + return( + map( + $items{$_}, + sort(keys(%items)) + ) + ); + +} + + + +################################################################################ +# Produce a printable string from a list of items +# Takes one argument: +# * A list of items, as an array of pairs of stock IDs and quantities (e.g. +# ( +# [ 1, 2 ], [ 4, 3 ], [ 3, 7 ] +# ) +# ) +# Returns: +# * A string represneting the items as they appeared in the input list (e.g. +# "[ 1, 2 ], [ 4, 3 ], [ 3, 7 ]" ). Note that if any additional exterior +# parentheses or braces are required, the caller must prepend/append these to +# the returned string. +################################################################################ +sub item_list_to_string{ + + return( + join( + ", ", + map( + "[ " + . + join( + ", ", + @{$_} + ) + . + " ]", + @ARG + ) + ) + ); + +} + + + |
