Re:Array Iteration Interview Problem
by spicyj on Wednesday August 01, 2012 @08:33PM
(#4326554)
My Python solution:
[ Parent
| on HN ]
One for ruby 1.9:
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