Skip to content

Instantly share code, notes, and snippets.

@pensebien
Last active September 29, 2016 22:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pensebien/94188c820604c2d14c5721672708e54e to your computer and use it in GitHub Desktop.
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]
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