aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-11-25 21:59:58 +0000
committerGitHub <noreply@github.com>2020-11-25 21:59:58 +0000
commite5d6cb29354ee3de6f0167f161063009513efde5 (patch)
treec0e577d91392b9030bf8d31df2d743cc9709828d
parentc87d80964bc180e2ee79c2b889d1f66cb1b40ca5 (diff)
parent195b3f09e6ab26fcd2b17d1450ea01015abcfbab (diff)
downloadperlweeklychallenge-club-e5d6cb29354ee3de6f0167f161063009513efde5.tar.gz
perlweeklychallenge-club-e5d6cb29354ee3de6f0167f161063009513efde5.tar.bz2
perlweeklychallenge-club-e5d6cb29354ee3de6f0167f161063009513efde5.zip
Merge pull request #2847 from Cris-HD/branch-for-challenge-088
Added challenge 88 solution
-rw-r--r--challenge-088/cristian-heredia/perl/ch-1.pl53
1 files changed, 53 insertions, 0 deletions
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";
+
+
+