Zurück zur Lektion

examples/05.py

Code und ursprüngliche Produktansichten behalten ihre Originalsprache. Die Lektion erklärt sie in deiner gewählten Sprache.

# Before: the conditions are correct but crowded onto one line.
def inside_grid_compact(grid, row, column):
    return bool(grid) and 0 <= row < len(grid) and 0 <= column < len(grid[0])

# After: preserve the evaluation order while exposing each condition.
def inside_grid(grid, row, column):
    return (
        bool(grid)
        and 0 <= row < len(grid)
        and 0 <= column < len(grid[0])
    )

assert inside_grid([[0]*3 for _ in range(2)], 1, 2)
assert not inside_grid([[0]*3 for _ in range(2)], 2, 0)
assert not inside_grid([], 0, 0)
for grid, row, column in (([], 0, 0), ([[0]*3]*2, 1, 2), ([[0]*3]*2, 2, 0)):
    assert inside_grid_compact(grid, row, column) == inside_grid(grid, row, column)