Skip to content

Instantly share code, notes, and snippets.

@jayceekay
Created June 7, 2016 18:27
Show Gist options
  • Save jayceekay/751630788deb5be2fc1e57d43c6eaa84 to your computer and use it in GitHub Desktop.
Save jayceekay/751630788deb5be2fc1e57d43c6eaa84 to your computer and use it in GitHub Desktop.
print a 2d matrix diagonally
def print_diag(matrix):
h = len(matrix)
w = len(matrix[0])
x = 0
y = 0
anchor = 0
while True:
if x >= w or y < 0:
anchor += 1
y = anchor
x = 0
print
if anchor >= h:
break
print matrix[y][x],
y -= 1
x += 1
anchor = 1
x = 1
y = h - 1
while True:
if x >= w or y < 0:
anchor += 1
x = anchor
y = h - 1
print
if anchor >= w:
break
print matrix[y][x],
y -= 1
x += 1
print_diag([[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment