← Back to challenges

Image Manipulation: Invert (Part 1)

PythonHardarraysloopslogicdata_structures

Instructions

Images can be described as a 3D list.

# This image has only one white pixel:

[
  [[255, 255, 255]]
]
# This one is a 2 by 2 black image:

[
  [[0, 0, 0], [0, 0, 0]],
  [[0, 0, 0], [0, 0, 0]]
]

Your task is to create a function that takes a 3D list representation of an image and returns the inverse of that.

For example, white becomes black, black becomes white, etc.

Examples

invert([
  [[255, 255, 255], [255, 255, 255]],
  [[255, 255, 255], [255, 255, 255]]
]) ➞ [
  [[0, 0, 0], [0, 0, 0]],
  [[0, 0, 0], [0, 0, 0]]
]

Notes

  • A valid RGB value ranges from 0 to 255 (inclusive).
  • If the given list contains out of bound values, convert them to the nearest valid one.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.