Last active
September 29, 2016 22:17
-
-
Save pensebien/94188c820604c2d14c5721672708e54e to your computer and use it in GitHub Desktop.
Find the product of the elements in an Array without the element itself. [1,2,3], newArray = [6,3,2]
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
def product_of_elements_array(a) | |
i = 0 | |
prod = 1 | |
while i <= (a.length-1) | |
prod = prod * a[i] | |
i+=1 | |
end | |
prod | |
end | |
def products_of_array(a) | |
i = 0 | |
arr = [] | |
tmp = 0 | |
while i<= a.length-1 | |
tmp = a[i] | |
a[i] = 1 | |
arr.push(product_of_elements_array(a)) | |
a[i] = tmp | |
i+=1 | |
end | |
arr | |
end | |
puts products_of_array([1,2,1,0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment