Skip to content

Instantly share code, notes, and snippets.

@coreyjs
Created February 25, 2019 18:52
Show Gist options
  • Select an option

  • Save coreyjs/25a0fabfada1512a56b5d4e37c5fbf4f to your computer and use it in GitHub Desktop.

Select an option

Save coreyjs/25a0fabfada1512a56b5d4e37c5fbf4f to your computer and use it in GitHub Desktop.
Daily Coding Email Problem 2
#Given an array of integers, return a new array such that
# each element at index i of the new array is the product of all the
# numbers in the original array except the one at i.
input = [1, 2, 3, 4, 5]
input2 = [3, 2, 1]
def sum_array(input)
total = input.reduce(1, :*)
out = []
puts "Total Value of input array: #{input}"
input.each_with_index do |num, index|
out[index] = (total * (num**-1)).ceil
end
out
end
puts sum_array(input).to_s
puts sum_array(input2).to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment