From 174f15dd260f75e4ad54e7490c6b41bf5e5fca53 Mon Sep 17 00:00:00 2001 From: princy <137168107+princyym@users.noreply.github.com> Date: Tue, 2 Apr 2024 14:41:55 +0530 Subject: #263 challeneg by princy mangla --- challenge-263/perl/ch-1.pl | 0 challenge-263/perl/ch-2.pl | 0 challenge-263/princy-perl/ch-1.pl | 28 ++++++++++++++++++++++++++++ challenge-263/princy-perl/ch-2.pl | 21 +++++++++++++++++++++ 4 files changed, 49 insertions(+) delete mode 100644 challenge-263/perl/ch-1.pl delete mode 100644 challenge-263/perl/ch-2.pl create mode 100644 challenge-263/princy-perl/ch-1.pl create mode 100644 challenge-263/princy-perl/ch-2.pl diff --git a/challenge-263/perl/ch-1.pl b/challenge-263/perl/ch-1.pl deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/challenge-263/perl/ch-2.pl b/challenge-263/perl/ch-2.pl deleted file mode 100644 index e69de29bb2..0000000000 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 -- cgit