Skip to content

Instantly share code, notes, and snippets.

@mtth
Created May 22, 2015 22:54
Show Gist options
  • Save mtth/8793f8f773fba82d94c1 to your computer and use it in GitHub Desktop.
Save mtth/8793f8f773fba82d94c1 to your computer and use it in GitHub Desktop.
def indices(m, n):
"""Index generator to visit a 2-dimensional matrix in increasing i + j.
:param m: Number of rows.
:param n: Number of columns.
"""
for i in range(m):
for j in range(min(n, i + 1)):
yield (i - j, j)
for j in range(1, n):
for i in range(m - 1, max(-1, m + j - n - 1), -1):
yield (i, m - 1 - i + j)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment