Created
June 7, 2016 18:27
-
-
Save jayceekay/751630788deb5be2fc1e57d43c6eaa84 to your computer and use it in GitHub Desktop.
print a 2d matrix diagonally
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
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 | |
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