Skip to content

Instantly share code, notes, and snippets.

@mferrier
Created August 2, 2012 00:30
Show Gist options
  • Save mferrier/3231896 to your computer and use it in GitHub Desktop.
Save mferrier/3231896 to your computer and use it in GitHub Desktop.
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