Skip to content

Instantly share code, notes, and snippets.

@DanKnox
Created August 2, 2012 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DanKnox/3232508 to your computer and use it in GitHub Desktop.
Save DanKnox/3232508 to your computer and use it in GitHub Desktop.
Array iteration interview question
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