|
| 1 | +def get_products_of_all_ints_except_at_index(lst): |
| 2 | + """ Takes a list of integers and returns a list of the products except the integer at that index. Do not use division. |
| 3 | +
|
| 4 | + >>> get_products_of_all_ints_except_at_index([1, 7, 3, 4]) |
| 5 | + [84, 12, 28, 21] |
| 6 | +
|
| 7 | + >>> get_products_of_all_ints_except_at_index([1, 0, 3, 4]) |
| 8 | + [0, 12, 0, 0] |
| 9 | + """ |
| 10 | + |
| 11 | + # Runtime: O(n^2) |
| 12 | + # Spacetime: O(n^2) |
| 13 | + |
| 14 | + products = [] |
| 15 | + |
| 16 | + for a in range(len(lst)): |
| 17 | + product = 1 |
| 18 | + |
| 19 | + for b in range(len(lst)): |
| 20 | + if a != b: |
| 21 | + product *= lst[b] |
| 22 | + products.append(product) |
| 23 | + |
| 24 | + return products |
| 25 | + |
| 26 | +# Testing |
| 27 | +# a = 0 |
| 28 | +# product = 1 |
| 29 | +# b = 0 |
| 30 | +# skip |
| 31 | +# b = 1 |
| 32 | +# product = 1 * 7 |
| 33 | +# b = 2 |
| 34 | +# product = 1 * 7 * 3 |
| 35 | +# b = 3 |
| 36 | +# product = 1 * 7 * 3 * 4 |
| 37 | + |
| 38 | +# products = [84] |
| 39 | + |
| 40 | +# a = 1 |
| 41 | +# product = 7 |
| 42 | + |
| 43 | +# b = 0 |
| 44 | +# product = 7 * 1 |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +def get_products_of_all_ints_except_at_index_optimized(lst): |
| 49 | + """ Takes a list of integers and returns a list of the products except the integer at that index. Do not use division. |
| 50 | +
|
| 51 | + >>> get_products_of_all_ints_except_at_index_optimized([1, 7, 3, 4]) |
| 52 | + [84, 12, 28, 21] |
| 53 | +
|
| 54 | + >>> get_products_of_all_ints_except_at_index_optimized([1, 0, 3, 4]) |
| 55 | + [0, 12, 0, 0] |
| 56 | + """ |
| 57 | + |
| 58 | + # Runtime: O(n) |
| 59 | + # Spacetime: O(n) |
| 60 | + |
| 61 | + products = [] |
| 62 | + product = 1 |
| 63 | + product_reverse = 1 |
| 64 | + products_before = [] |
| 65 | + products_after = [] |
| 66 | + |
| 67 | + for i in range(len(lst)): |
| 68 | + products_before.append(product) |
| 69 | + product *= lst[i] |
| 70 | + |
| 71 | + for i in range(len(lst)-1, -1, -1): |
| 72 | + products_after.append(product_reverse) |
| 73 | + product_reverse *= lst[i] |
| 74 | + |
| 75 | + for i in range(len(products_before)): |
| 76 | + products.append(products_after[-i-1] * products_before[i]) |
| 77 | + |
| 78 | + return products |
| 79 | + |
| 80 | + |
| 81 | +# Testing |
| 82 | +# lst = [1, 7, 3, 4] |
| 83 | + |
| 84 | +# first for loop |
| 85 | +# i = 0 |
| 86 | +# products_before = [1] |
| 87 | +# product = 1 |
| 88 | +# i = 1 |
| 89 | +# products_before = [1, 1] |
| 90 | +# product = 7 |
| 91 | +# i = 2 |
| 92 | +# products_before = [1, 1, 7] |
| 93 | +# product = 21 |
| 94 | +# i = 3 |
| 95 | +# products_before = [1, 1, 7, 21] |
| 96 | +# product = 84 |
| 97 | +# i = 4 |
| 98 | + |
| 99 | +# second for loop |
| 100 | +# i = 3 |
| 101 | +# products_after = [1] |
| 102 | +# product_reverse = 4 |
| 103 | +# i = 2 |
| 104 | +# products_after [1, 4] |
| 105 | +# product_reverse = 12 |
| 106 | +# i = 1 |
| 107 | +# products_after = [1, 4, 12] |
| 108 | +# product_reverse = 84 |
| 109 | +# i = 0 |
| 110 | +# products_after = [1, 4, 12, 84] |
| 111 | +# product_reverse = 84 |
| 112 | +# i = -1 |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == '__main__': |
| 118 | + import doctest |
| 119 | + results = doctest.testmod() |
| 120 | + |
| 121 | + if results.failed == 0: |
| 122 | + print "ALL TESTS PASSED" |
| 123 | + |
0 commit comments