Skip to content

Instantly share code, notes, and snippets.

@gregorykremler
Last active August 29, 2015 14:10
Show Gist options
  • Save gregorykremler/6289507b8799dd0993e5 to your computer and use it in GitHub Desktop.
Save gregorykremler/6289507b8799dd0993e5 to your computer and use it in GitHub Desktop.
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