diff options
| -rw-r--r-- | challenge-263/princy-perl/ch-1.pl | 28 | ||||
| -rw-r--r-- | challenge-263/princy-perl/ch-2.pl | 21 |
2 files changed, 49 insertions, 0 deletions
diff --git a/challenge-263/princy-perl/ch-1.pl b/challenge-263/princy-perl/ch-1.pl new file mode 100644 index 0000000000..55226c5e2f --- /dev/null +++ b/challenge-263/princy-perl/ch-1.pl @@ -0,0 +1,28 @@ + +#!/usr/bin/perl + +use strict; +use warnings; + +# Input array and target +my @ints = (1, 5, 3, 2, 4, 2); +my $k = 2; + +# Sorta the array +my @sorted = sort @ints; + +# Initialize an empty array to store the indices +my @indices; + +# Iterate over the sorted array +for (my $i = 0; $i < @sorted; $i++) { + # If the current element is equal to the target + if ($sorted[$i] == $k) { + # Add the index to the array + push @indices, $i; + } +} + +# Print the indices +print "The indices where the element is $k are: @indices\n"; + diff --git a/challenge-263/princy-perl/ch-2.pl b/challenge-263/princy-perl/ch-2.pl new file mode 100644 index 0000000000..53d6eadcf5 --- /dev/null +++ b/challenge-263/princy-perl/ch-2.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +# Input arrays +my @items1 = ([1,1], [2,1], [3,2] ); +my @items2 = ( + [2,2], [1,3]); + +# Merge the arrays +my %merged_items; +foreach my $item (@items1, @items2) { + my ($id, $quantity) = @$item; + $merged_items{$id} += $quantity; +} + +# Print the merged items +foreach my $id (sort keys %merged_items) { + print "($id,$merged_items{$id})\n"; +}
\ No newline at end of file |
