-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDC__2__.py
More file actions
127 lines (81 loc) · 3.31 KB
/
DC__2__.py
File metadata and controls
127 lines (81 loc) · 3.31 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""
September 19, 2018
This problem was asked by Uber.
Given an array of integers, return a new array such that each element at index i of the new array is the product of all the numbers in the original array except the one at i.
For example, if our input was [1, 2, 3, 4, 5], the expected output would be [120, 60, 40, 30, 24]. If our input was [3, 2, 1], the expected output would be [2, 3, 6].
Follow-up: what if you can't use division?
"""
def smart_array(X):
# Test for a valid input
if (X.__len__() <= 0):
return []
# No numbers arround the single number in X
if (X.__len__() == 1):
return [0]
ris = []
# 1st PASS - consider the numbers on the left side the i-th number
bag = 1
for x in X:
ris.append(bag)
bag *= x
#print("DEBUG: ris_1st_pass =", ris, "\n")
# 2nd PASS - consider the numbers on the right side of the i-th number (iterating in reverse order)
bag = 1
for i in range(-1, -1*(len(X)+1), -1):
ris[i] *= bag
bag *= X[i]
#print("DEBUG: ris_2nd_pass =", ris, "\n")
return ris
"""
========================================================================
=============================== TESTING ================================
========================================================================
"""
print("\n'''=====================<Test 1 - begin>===================='''")
X = [1, 2, 3, 4]
ris = smart_array(X)
print("X =", X, "\n")
print("smart_array = <", ris, "> \n")
print("'''=====================<Test 1 - end>======================'''\n")
print("\n'''=====================<Test 2 - begin>===================='''")
X = []
ris = smart_array(X)
print("X =", X, "\n")
print("smart_array = <", ris, "> \n")
print("'''=====================<Test 2 - end>======================'''\n")
print("\n'''=====================<Test 3 - begin>===================='''")
X = [0]
ris = smart_array(X)
print("X =", X, "\n")
print("smart_array = <", ris, "> \n")
print("'''=====================<Test 3 - end>======================'''\n")
print("\n'''=====================<Test 4 - begin>===================='''")
X = [0, 1, 2, 3, 4]
ris = smart_array(X)
print("X =", X, "\n")
print("smart_array = <", ris, "> \n")
print("'''=====================<Test 4 - end>======================'''\n")
print("\n'''=====================<Test 5 - begin>===================='''")
X = [0, 1, 2, 3, 4, 0]
ris = smart_array(X)
print("X =", X, "\n")
print("smart_array = <", ris, "> \n")
print("'''=====================<Test 5 - end>======================'''\n")
print("\n'''=====================<Test 6 - begin>===================='''")
X = [1, 2, 0, 3, 4]
ris = smart_array(X)
print("X =", X, "\n")
print("smart_array = <", ris, "> \n")
print("'''=====================<Test 6 - end>======================'''\n")
print("\n'''=====================<Test 7 - begin>===================='''")
X = [1, 2, 3, 4, 1, 2, 3, 4]
ris = smart_array(X)
print("X =", X, "\n")
print("smart_array = <", ris, "> \n")
print("'''=====================<Test 7 - end>======================'''\n")
print("\n'''=====================<Test 8 - begin>===================='''")
X = [2, 2, 2, 0, 2, 2, 2]
ris = smart_array(X)
print("X =", X, "\n")
print("smart_array = <", ris, "> \n")
print("'''=====================<Test 8 - end>======================'''\n")