examples/05.py
Kode n’amashusho y’umwimerere ya porogaramu bigumana ururimi rwabyo. Isomo ribisobanura mu rurimi wahisemo.
# 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)