SHARE
TWEET

Untitled

a guest May 27th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from typing import List
  2.  
  3.  
  4. def traverse_matrix(matrix: List[List[int]]) -> List[int]:
  5.     n = len(matrix)
  6.     def trav_rec(n, matrix):
  7.         if n == 0:
  8.             return []
  9.         if n == 1:
  10.             return [matrix[0][0]]
  11.         if n == 2:
  12.             return [matrix[0][0], matrix[0][1], matrix[1][1], matrix[1][0]]
  13.         result = []
  14.         result.extend(matrix[0])
  15.         result.extend([matrix[i][-1] for i in range(1, n - 1)])
  16.         result.extend(reversed(matrix[-1]))
  17.         result.extend(reversed([matrix[i][0] for i in range(1, n-1)]))
  18.         mini_matrix = [matrix[i][1:n-1] for i in range(1, n - 1)]
  19.         result.extend(trav_rec(n - 2, mini_matrix))
  20.         return result
  21.     return trav_rec(n, matrix)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top