Created
April 4, 2014 22:00
-
-
Save thomasgwatson/9983924 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 find_deviation(v, d) | |
max_dev = 0 | |
iterations = v.length - d + 1 | |
index = 0 | |
iterations.times do | |
sub = v[index...index+d] | |
sub_max_dev = sub.max - sub.min | |
max_dev = sub_max_dev if sub_max_dev > max_dev | |
index +=1 | |
end | |
p max_dev | |
end | |
v = [6,9,4,7,4,1] | |
d = 3 | |
find_deviation(v,d) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Andy's solution is by far the fastest. If you wanted to get even faster use a for loop vs an each (2x-4x faster).