Created
March 9, 2012 08:30
-
-
Save wulfgarpro/2005650 to your computer and use it in GitHub Desktop.
Solution to tutorial 1, q2
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
# 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