forked from learning-zone/python-basics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
products_of_all_ints.py
123 lines (91 loc) · 2.32 KB
/
products_of_all_ints.py
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
def get_products_of_all_ints_except_at_index(lst):
""" Takes a list of integers and returns a list of the products except the integer at that index. Do not use division.
>>> get_products_of_all_ints_except_at_index([1, 7, 3, 4])
[84, 12, 28, 21]
>>> get_products_of_all_ints_except_at_index([1, 0, 3, 4])
[0, 12, 0, 0]
"""
# Runtime: O(n^2)
# Spacetime: O(n^2)
products = []
for a in range(len(lst)):
product = 1
for b in range(len(lst)):
if a != b:
product *= lst[b]
products.append(product)
return products
# Testing
# a = 0
# product = 1
# b = 0
# skip
# b = 1
# product = 1 * 7
# b = 2
# product = 1 * 7 * 3
# b = 3
# product = 1 * 7 * 3 * 4
# products = [84]
# a = 1
# product = 7
# b = 0
# product = 7 * 1
def get_products_of_all_ints_except_at_index_optimized(lst):
""" Takes a list of integers and returns a list of the products except the integer at that index. Do not use division.
>>> get_products_of_all_ints_except_at_index_optimized([1, 7, 3, 4])
[84, 12, 28, 21]
>>> get_products_of_all_ints_except_at_index_optimized([1, 0, 3, 4])
[0, 12, 0, 0]
"""
# Runtime: O(n)
# Spacetime: O(n)
products = []
product = 1
product_reverse = 1
products_before = []
products_after = []
for i in range(len(lst)):
products_before.append(product)
product *= lst[i]
for i in range(len(lst)-1, -1, -1):
products_after.append(product_reverse)
product_reverse *= lst[i]
for i in range(len(products_before)):
products.append(products_after[-i-1] * products_before[i])
return products
# Testing
# lst = [1, 7, 3, 4]
# first for loop
# i = 0
# products_before = [1]
# product = 1
# i = 1
# products_before = [1, 1]
# product = 7
# i = 2
# products_before = [1, 1, 7]
# product = 21
# i = 3
# products_before = [1, 1, 7, 21]
# product = 84
# i = 4
# second for loop
# i = 3
# products_after = [1]
# product_reverse = 4
# i = 2
# products_after [1, 4]
# product_reverse = 12
# i = 1
# products_after = [1, 4, 12]
# product_reverse = 84
# i = 0
# products_after = [1, 4, 12, 84]
# product_reverse = 84
# i = -1
if __name__ == '__main__':
import doctest
results = doctest.testmod()
if results.failed == 0:
print "ALL TESTS PASSED"