All problems
Reflect Padding
Problem
Pad an image by reflecting it across its edges without repeating the edge pixel (reflect mode).
With one row [a, b, c] and pad 1 the padded row is [b, a, b, c, b]. Apply the same rule to rows and columns; pad may be 0. You may assume each dimension is at least 2 when pad is positive.
Hint
Map index -1 to 1 and index h to h - 2; use a while loop so large pads work.
Test cases
case 1
in: [[[1,2],[3,4]],1]
expected: [[4,3,4,3],[2,1,2,1],[4,3,4,3],[2,1,2,1]]
case 2
in: [[[1,2,3],[4,5,6]],1]
expected: [[5,4,5,6,5],[2,1,2,3,2],[5,4,5,6,5],[2,1,2,3,2]]
case 3
in: [[[9,8,7],[6,5,4],[3,2,1]],1]
expected: [[5,6,5,4,5],[8,9,8,7,8],[5,6,5,4,5],[2,3,2,1,2],[5,6,5,4,5]]
case 4
in: [[[1,2],[3,4]],0]
expected: [[1,2],[3,4]]
Starter codePython
def reflect_pad(image, pad):
# Your code here
pass4 test cases · run your code against them in the editor