diff options
| -rwxr-xr-x | challenge-263/peter-meszaros/perl/ch-1.pl | 67 | ||||
| -rwxr-xr-x | challenge-263/peter-meszaros/perl/ch-2.pl | 69 |
2 files changed, 136 insertions, 0 deletions
diff --git a/challenge-263/peter-meszaros/perl/ch-1.pl b/challenge-263/peter-meszaros/perl/ch-1.pl new file mode 100755 index 0000000000..1b2128d8b0 --- /dev/null +++ b/challenge-263/peter-meszaros/perl/ch-1.pl @@ -0,0 +1,67 @@ +#!/usr/bin/env perl +# +# 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 $k[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 +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[[1, 5, 3, 2, 4, 2], 2], [1, 2], 'Example 1'], + [[[1, 2, 4, 3, 5], 6], [], 'Example 2'], + [[[5, 3, 2, 4, 2, 1], 4], [4], 'Example 3'], +]; + +sub target_index +{ + my $l = $_->[0]->[0]; + my $k = $_->[0]->[1]; + + my @l = sort {$a <=> $b} @$l; + my @res; + + my $found; + for (0..$#l) { + if ($l[$_] == $k) { + push @res, $_; + $found = 1; + } elsif ($found) { + last; + } + } + return \@res; +} + +for (@$cases) { + is(target_index($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; diff --git a/challenge-263/peter-meszaros/perl/ch-2.pl b/challenge-263/peter-meszaros/perl/ch-2.pl new file mode 100755 index 0000000000..4b3cad77ac --- /dev/null +++ b/challenge-263/peter-meszaros/perl/ch-2.pl @@ -0,0 +1,69 @@ +#!/usr/bin/env perl +# +# 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] ] +# + +use strict; +use warnings; +use Test2::V0 -no_srand => 1; +use Data::Dumper; + +my $cases = [ + [[[[1,1], [2,1], [3,2]], [[2,2], [1,3]]], + [[1,4], [2,3], [3,2]], 'Example 1'], + [[[[1,2], [2,3], [1,3], [3,2]], [[3,1], [1,3]]], + [[1,8], [2,3], [3,3]], 'Example 2'], + [[[[1,1], [2,2], [3,3]], [[2,3], [2,4]]], + [[1,1], [2,9], [3,3]], 'Example 3'], +]; + +sub merge_items +{ + my $l1 = $_->[0]->[0]; + my $l2 = $_->[0]->[1]; + + my %l; + + for my $i (@$l1, @$l2) { + $l{$i->[0]} += $i->[1]; + } + + my @res; + for my $k (sort {$a <=> $b} keys %l) { + push @res, [$k, $l{$k}]; + } + + return \@res; +} + +for (@$cases) { + is(merge_items($_->[0]), $_->[1], $_->[2]); +} +done_testing(); + +exit 0; + |
