forked from y-ncao/Python-Study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrint_Matrix.py
More file actions
40 lines (35 loc) · 769 Bytes
/
Print_Matrix.py
File metadata and controls
40 lines (35 loc) · 769 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
"""
#####From [mitbbs](http://www.mitbbs.com/article_t/JobHunting/32570751.html) for Pinterest
Print a N x M matrix in diagonal from the upper left to lower right. However, with the following caveat. It's easy to just show the input and expect output.
```
matrix:
a b c d
e f g h
i j k l
m n o p
output:
a f k p
b g l
c h
d
e j o
i n
m
```
"""
def print_matrix(matrix):
N = len(matrix)
for i in range(N):
start = 0
while start + i < N:
print matrix[start][i+start],
start += 1
print '\n'
for i in range(1, N):
start = 0
while start + i < N:
print matrix[i+start][start],
start += 1
print '\n'
matrix = ['abcd', 'efgh', 'ijkl', 'mnop']
print_matrix(matrix)