diff options
| author | princy <137168107+princyym@users.noreply.github.com> | 2024-04-02 14:41:55 +0530 |
|---|---|---|
| committer | princy <137168107+princyym@users.noreply.github.com> | 2024-04-02 14:41:55 +0530 |
| commit | 174f15dd260f75e4ad54e7490c6b41bf5e5fca53 (patch) | |
| tree | 17c9374d9b7027c2b9af89fb718ef68e25890091 | |
| parent | 130f38aeb59be75717f439bfbdc665e4287630f7 (diff) | |
| download | perlweeklychallenge-club-174f15dd260f75e4ad54e7490c6b41bf5e5fca53.tar.gz perlweeklychallenge-club-174f15dd260f75e4ad54e7490c6b41bf5e5fca53.tar.bz2 perlweeklychallenge-club-174f15dd260f75e4ad54e7490c6b41bf5e5fca53.zip | |
#263 challeneg by princy mangla
| -rw-r--r-- | challenge-263/perl/ch-1.pl | 0 | ||||
| -rw-r--r-- | challenge-263/perl/ch-2.pl | 0 | ||||
| -rw-r--r-- | challenge-263/princy-perl/ch-1.pl | 28 | ||||
| -rw-r--r-- | challenge-263/princy-perl/ch-2.pl | 21 |
4 files changed, 49 insertions, 0 deletions
diff --git a/challenge-263/perl/ch-1.pl b/challenge-263/perl/ch-1.pl deleted file mode 100644 index e69de29bb2..0000000000 --- a/challenge-263/perl/ch-1.pl +++ /dev/null diff --git a/challenge-263/perl/ch-2.pl b/challenge-263/perl/ch-2.pl deleted file mode 100644 index e69de29bb2..0000000000 --- a/challenge-263/perl/ch-2.pl +++ /dev/null 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 |
