Print matrix in diagonal pattern | Initialize matrix ; n - size mode - switch to derive up / down traversal it - iterator count - increases until it reaches n and then decreases ; 2 n will be the number of iterations
mat = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] ; NEW_LINE n = 4 NEW_LINE mode = 0 NEW_LINE it = 0 NEW_LINE lower = 0 NEW_LINE for t in range ( 2 * n - 1 ) : NEW_LINE INDENT t1 = t NEW_LINE if ( t1 >= n ) : NEW_LINE INDENT mode += 1 NEW_LINE t1 = n - 1 NEW_LINE it -= 1 NEW_LINE lower += 1 NEW_LINE DEDENT else : NEW_LINE INDENT lower = 0 NEW_LINE it += 1 NEW_LINE DEDENT for i in range ( t1 , lower - 1 , - 1 ) : NEW_LINE INDENT if ( ( t1 + mode ) % 2 == 0 ) : NEW_LINE INDENT print ( ( mat [ i ] [ t1 + lower - i ] ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( mat [ t1 + lower - i ] [ i ] ) NEW_LINE DEDENT DEDENT DEDENT