Skip to content

Instantly share code, notes, and snippets.

@wulfgarpro
Created March 9, 2012 08:30
Show Gist options
  • Save wulfgarpro/2005650 to your computer and use it in GitHub Desktop.
Save wulfgarpro/2005650 to your computer and use it in GitHub Desktop.
Solution to tutorial 1, q2
# this is the O(n) solution for tutorial 1, question 2 in ruby
A = [2,4,6,8] # elements to multiply
B = [] # left products
C = [] # right products
output = [] # to hold final result
product = 1
A.each_with_index {|x, y| B[y] = product; product *= A[y]}
product = 1
A.each_with_index {|x, y| C[A.length-y-1] = product; product *= A[A.length-y-1]}
A.each_with_index {|x, y| output[y] = B[y] * C[y]}
puts(output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment