From 195b3f09e6ab26fcd2b17d1450ea01015abcfbab Mon Sep 17 00:00:00 2001 From: Cris-HD Date: Wed, 25 Nov 2020 18:43:47 +0100 Subject: Added challenge 88 solution --- challenge-088/cristian-heredia/perl/ch-1.pl | 53 +++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 challenge-088/cristian-heredia/perl/ch-1.pl diff --git a/challenge-088/cristian-heredia/perl/ch-1.pl b/challenge-088/cristian-heredia/perl/ch-1.pl new file mode 100644 index 0000000000..0ba2a83250 --- /dev/null +++ b/challenge-088/cristian-heredia/perl/ch-1.pl @@ -0,0 +1,53 @@ +=begin +TASK #1 › Array of Product +Submitted by: Mohammad S Anwar +You are given an array of positive integers @N. + +Write a script to return an array @M where $M[i] is the product of all elements of @N except the index $N[i]. + +Example 1: +Input: + @N = (5, 2, 1, 4, 3) +Output: + @M = (24, 60, 120, 30, 40) + + $M[0] = 2 x 1 x 4 x 3 = 24 + $M[1] = 5 x 1 x 4 x 3 = 60 + $M[2] = 5 x 2 x 4 x 3 = 120 + $M[3] = 5 x 2 x 1 x 3 = 30 + $M[4] = 5 x 2 x 1 x 4 = 40 +Example 2: +Input: + @N = (2, 1, 4, 3) +Output: + @M = (12, 24, 6, 8) + + $M[0] = 1 x 4 x 3 = 12 + $M[1] = 2 x 4 x 3 = 24 + $M[2] = 2 x 1 x 3 = 6 + $M[3] = 2 x 1 x 4 = 8 +=end +=cut + +use strict; +use warnings; +use Data::Dumper; + +my @N = (5, 2, 1, 4, 3); +my $result = 1; +my @temp; +my @M; + +foreach(my $i=0;$i<@N;$i++){ + @temp = @N; + splice(@temp, $i, 1); + $result = 1; + foreach(my $j=0;$j<@temp;$j++) { + $result = $temp[$j] * $result; + } + push @M, $result; +} +print "Output: (", (join ", ", @M), ")\n"; + + + -- cgit