News for hackers. Stuff that matters.
Re:Array Iteration Interview Problem by mef on Wednesday August 01, 2012 @08:42PM (#4326581)
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

There are no comments on this comment yet.