Created
November 12, 2014 14:15
-
-
Save binhngoc17/5a74374d80b93c19b1b2 to your computer and use it in GitHub Desktop.
Diagonal Matrix
This file contains hidden or 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
''' | |
Notes about this problem: | |
- Try to analyze the formation of the result & a way to construct it | |
''' | |
M = [[1, 2, 3], | |
[4, 5, 6], | |
[7, 8, 9]] | |
def mat_diagonal(M): | |
res = [] | |
n = len(M) | |
for i in range(0, n * 2 - 1): | |
data = [] | |
for j in range(0, n): | |
if i - j < 0 or i - j >= n: | |
continue | |
data.append(M[i - j][j]) | |
res.append(data) | |
return res | |
assert mat_diagonal(M) == [ [1], [4, 2], [7, 5, 3], [8, 6], [9]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment