Created
August 2, 2012 02:12
-
-
Save DanKnox/3232508 to your computer and use it in GitHub Desktop.
Array iteration interview question
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) | |
stop = arr.length - 1 | |
count = 0 | |
(0..stop).each do |i| | |
index = n < 0 ? stop - i : i | |
if arr[index] == val | |
arr[index] += 1 | |
count += 1 | |
end | |
break if count == n.abs and n != 0 | |
end | |
arr | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment