Created
October 1, 2016 15:11
-
-
Save JuliusNM/34371935ddd648643c31faa198306cdc 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
def product_array(arr) | |
prd=1 | |
arr.each do |x| | |
x > 0 ? prd= prd*x : prd = prd | |
end | |
prd | |
end | |
def new_array (arr) | |
zero_count = arr.count(0) | |
arry = [] | |
if zero_count == 1 | |
prd = product_array (arr) | |
arr.each {|i| i == 0 ? arry.push(prd) : arry.push(0) } | |
elsif zero_count > 1 | |
arr.each {|i| arry.push(0)} | |
else | |
y = product_array (arr) | |
arr.each do |x| | |
arry.push(y/x) | |
end | |
end | |
arry | |
end | |
a = [1,1,5,2] | |
ans = new_array(a) | |
puts ("#{ans}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment