Skip to content

Instantly share code, notes, and snippets.

@RickArora
Created December 31, 2018 17:12
Show Gist options
  • Save RickArora/8f4cf03666bb04a4aafcb3def180cb8a to your computer and use it in GitHub Desktop.
Save RickArora/8f4cf03666bb04a4aafcb3def180cb8a to your computer and use it in GitHub Desktop.
# products_except_me([1, 2, 3,,, 5]) => [30, 15, 10, 6], where: 30 because you
# take out 1, leaving 2 * 3 * 5 15, because you take out 2, leaving 1 * 3 * 5
# 10, because you take out 3, leaving 1 * 2 * 5 6, because you take out 5,
# leaving 1 * 2 * 3
def products_except_me(numbers)
copy_of_numbers = numbers #makes a copy of numbers
numbers.each.with_index {|int, i| copy_of_numbers[i] = array_product(numbers,int) }
end
def array_product(array, i)
counter = 1
array.map { |int| counter *= int }
return counter = counter / i
end
Failing Tests
Failures:
1) Enumerables part 1 products_except_me calculates the correct products
Failure/Error: expect(products_except_me(nums)).to eq(prods)
expected: [24, 12, 8, 6]
got: [24, 288, 27648, 191102976]
(compared using ==)
# ./spec/enumerables1_spec.rb:95:in `block (3 levels) in <top (required)>'
Finished in 0.03032 seconds (files took 0.1822 seconds to load)
15 examples, 1 failure
Failed examples:
rspec ./spec/enumerables1_spec.rb:92 # Enumerables part 1 products_except_me calculates the correct products
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment