diff options
| author | Cris-HD <crisn7@hotmail.com> | 2020-11-25 18:43:47 +0100 |
|---|---|---|
| committer | Cris-HD <crisn7@hotmail.com> | 2020-11-25 18:43:47 +0100 |
| commit | 195b3f09e6ab26fcd2b17d1450ea01015abcfbab (patch) | |
| tree | 7c2ba88d182baded8a4d693311b18a37568d9e3b | |
| parent | 89360d10a5f2c28dfaa7524a737129ebe9dbd23a (diff) | |
| download | perlweeklychallenge-club-195b3f09e6ab26fcd2b17d1450ea01015abcfbab.tar.gz perlweeklychallenge-club-195b3f09e6ab26fcd2b17d1450ea01015abcfbab.tar.bz2 perlweeklychallenge-club-195b3f09e6ab26fcd2b17d1450ea01015abcfbab.zip | |
Added challenge 88 solution
| -rw-r--r-- | challenge-088/cristian-heredia/perl/ch-1.pl | 53 |
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"; + + + |
