News for hackers. Stuff that matters.
Re:Array Iteration Interview Problem by spicyj on Wednesday August 01, 2012 @08:33PM (#4326554)
My Python solution:

    def add1(arr, val, n):
        it = xrange(len(arr))
        if n < 0:
            it = reversed(it)
            n *= -1

        found = 0
        for i in it:
            if arr[i] == val:
                arr[i] += 1
                found += 1
                if found == n:
                    break

        return arr
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