Skip to content

Instantly share code, notes, and snippets.

@binhngoc17
Created November 12, 2014 14:15
Show Gist options
  • Save binhngoc17/5a74374d80b93c19b1b2 to your computer and use it in GitHub Desktop.
Save binhngoc17/5a74374d80b93c19b1b2 to your computer and use it in GitHub Desktop.
Diagonal Matrix
'''
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