aboutsummaryrefslogtreecommitdiff
path: root/challenge-088/paulo-custodio/python/ch-1.py
blob: c63ae8f4be87db4af05b6a3c98814b8290c0a1e5 (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
#!/usr/bin/env python3

# Challenge 088
#
# 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

import sys

def array_product(n):
    m = [1 for x in n]          # initialize the products to 1
    for i in range(len(n)):
        for j in range(len(m)):
            if i!=j:
                m[j] *= n[i]
    return m

m = array_product([int(x) for x in sys.argv[1:]])
print("("+ ", ".join([str(x) for x in m]) +")")