Skip to content

Instantly share code, notes, and snippets.

@tylerkahn
Created August 2, 2012 00:54
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 tylerkahn/3232056 to your computer and use it in GitHub Desktop.
Save tylerkahn/3232056 to your computer and use it in GitHub Desktop.
def add1(arr, val, n)
if n.zero?
return arr.map! {|i| i == val ? i+1 : i }
else
arr.each_index do |j|
if arr[n < 0 ? -j - 1 : j] == val
arr[n < 0 ? -j - 1 : j] += 1
n = n - (n < 0 ? -1 : 1)
return arr if n.zero?
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment