Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. def get_products_of_all_ints_except_at_index(int_array):
  4.  
  5. # we make an array with the length of the input array to
  6. # hold our products
  7. products_of_all_ints_except_at_index = [1] * len(int_array)
  8.  
  9. # for each integer, we find the product of all the integers
  10. # before it, storing the total product so far each time
  11. product = 1
  12. i = 0
  13. while i < len(int_array):
  14. products_of_all_ints_except_at_index[i] = product
  15. product *= int_array[i]
  16. i += 1
  17.  
  18. # for each integer, we find the product of all the integers
  19. # after it. since each index in products already has the
  20. # product of all the integers before it, now we're storing
  21. # the total product of all other integers
  22. product = 1
  23. i = len(int_array) - 1
  24. while i >= 0:
  25. products_of_all_ints_except_at_index[i] *= product
  26. product *= int_array[i]
  27. i -= 1
  28.  
  29. return products_of_all_ints_except_at_index
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement