Skip to content

Instantly share code, notes, and snippets.

@ephekt
Forked from DanKnox/array_iteration.rb
Created August 2, 2012 02:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ephekt/3232582 to your computer and use it in GitHub Desktop.
Save ephekt/3232582 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