aboutsummaryrefslogtreecommitdiff
path: root/challenge-088/duane-powell/perl/ch-1.pl
blob: 3ac1fa776da0d648c9289a2fcffe01830e8b8314 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/perl
use warnings;
use strict;
use feature 'say';

# Problem: https://perlweeklychallenge.org/blog/perl-weekly-challenge-088/ TASK #1
# 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].

my @N = @ARGV;

my @M;
my $i = 0;
foreach ( @N ) {
	my @temp = @N;
	splice @temp, $i, 1;
	push @M, \@temp;
	$i++;
}

my @total;
foreach my $array_ref ( @M ) {
	my $total = 1;
	$total *= $_ foreach (@{$array_ref});
	push @total, $total;
}	

my $out;
say "Input:";
$out = join(', ',@N);
say "\t\@N = ($out)";

say "Output:";
$out = join(', ',@total);
say "\t\@M = ($out)\n";

$i = 0;
foreach my $array_ref ( @M ) {
	$out = "\t\$M[$i] = " . join(' x ', @{$array_ref}) . " = " . $total[$i];
	say $out;
	$i++;
}


__END__

./ch-1.pl 5 2 1 4 3
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


./ch-1.pl 2 1 4 3
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