All problems
Mirror Padding
Problem
Pad an image by mirroring it across its edges including the edge pixels (symmetric mode).
With one row [a, b, c] and pad 1 the padded row is [a, a, b, c, c]. Apply the same rule to rows and columns; each dimension is at least 2 when pad is positive.
Hint
Fold the index modulo 2n so -1 maps to 0 and n maps to n - 1.
Test cases
case 1
in: [[[1,2],[3,4]],1]
expected: [[1,1,2,2],[1,1,2,2],[3,3,4,4],[3,3,4,4]]
case 2
in: [[[1,2,3],[4,5,6]],1]
expected: [[1,1,2,3,3],[1,1,2,3,3],[4,4,5,6,6],[4,4,5,6,6]]
case 3
in: [[[1,2],[3,4]],0]
expected: [[1,2],[3,4]]
case 4
in: [[[1,2,3],[4,5,6],[7,8,9]],1]
expected: [[1,1,2,3,3],[1,1,2,3,3],[4,4,5,6,6],[7,7,8,9,9],[7,7,8,9,9]]
Starter codePython
def mirror_pad(image, pad):
# Your code here
pass4 test cases · run your code against them in the editor