Flipping an Image
Explore techniques to flip and invert an image represented by an n by n matrix of ones and zeros. Learn how to perform a horizontal flip and then invert the binary values using efficient bitwise manipulation. This lesson helps you understand the problem statement, develop a solution strategy, and implement it in a hands-on coding environment.
We'll cover the following...
Statement
Given that an image is represented by an matrix containing s and s, flip and invert the image, and return the resultant image.
Horizontally flipping an image means that the mirror image of the matrix should be returned. Flipping horizontally results in .
Inverting an image means that every is replaced by , and every is replaced by . Inverting results in .
Constraints:
- Image should be a square matrix.
images[i][j]is either or .
Examples
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Flipping an Image
Consider the following matrix as input. What is the output once it has been flipped and inverted?
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Try it yourself
Implement your solution in the following coding playground.
def flip_and_invert_image(image):# Replace this placeholder return statement with your codereturn image