Created
August 2, 2012 00:30
-
-
Save mferrier/3231896 to your computer and use it in GitHub Desktop.
array iteration problem http://blog.jazzychad.net/2012/08/01/array-iteration-problem.html
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 add1(arr, val, n) | |
limit = (n == 0 ? arr.length : n.abs) | |
enumerator = (n < 0 ? (arr.length-1).downto(0) : (0).upto(arr.length-1)) | |
enumerator.each do |i| | |
if arr[i] == val | |
arr[i] += 1 | |
limit -= 1 | |
end | |
break if limit == 0 | |
end | |
arr | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment