Last active
August 29, 2015 14:10
-
-
Save gregorykremler/6289507b8799dd0993e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from operator import mul | |
def get_products_of_all_ints_except_at_index(lst): | |
""" | |
Returns array of products of all ints except index, for given array of ints. | |
e.g., [1, 7, 3, 4] returns [84, 12, 28, 21] by calculating [7*3*4, 1*3*4, 1*7*4, 1*7*3] | |
""" | |
product_lst = [] | |
for idx in range(len(lst)): | |
except_at_index = lst[:idx] + lst[idx+1:] | |
product_lst.append(reduce(mul, except_at_index)) | |
return product_lst | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment