← Back to challenges

Crop Fields

PythonHardarraysconditionsloopsstringsvalidation

Instructions

You're given a 2D list / matrix of a crop field. Each crop needs a water source. Each water source hydrates the 8 tiles around it. With "w" representing a water source, and "c" representing a crop, is every crop hydrated?

Examples

crop_hydrated([
  [ "w", "c" ],
  [ "w", "c" ],
  [ "c", "c" ]
]) ➞ True

crop_hydrated([
  [ "c", "c", "c" ]
]) ➞ False
# There isn"t even a water source.

crop_hydrated([
  [ "c", "c", "c", "c" ],
  [ "w", "c", "c", "c" ],
  [ "c", "c", "c", "c" ],
  [ "c", "w", "c", "c" ]
]) ➞ False

Notes

"w" on its own should return True, and "c" on its own should return False.

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.