aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-05-01 09:01:58 +0100
committerGitHub <noreply@github.com>2024-05-01 09:01:58 +0100
commit189c7375291b773f2fb6894d4eff1e6582e5fffe (patch)
treed92476fd08fa0f75a89140b77355f8a821c90a88
parent24c440967580addc0710f643a21ed130278c2fcc (diff)
parent174f15dd260f75e4ad54e7490c6b41bf5e5fca53 (diff)
downloadperlweeklychallenge-club-189c7375291b773f2fb6894d4eff1e6582e5fffe.tar.gz
perlweeklychallenge-club-189c7375291b773f2fb6894d4eff1e6582e5fffe.tar.bz2
perlweeklychallenge-club-189c7375291b773f2fb6894d4eff1e6582e5fffe.zip
Merge pull request #9857 from princyym/branch-for-challenge-263
Branch for challenge 263
-rw-r--r--challenge-263/princy-perl/ch-1.pl28
-rw-r--r--challenge-263/princy-perl/ch-2.pl21
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