th0m45s5helby

matrix

Feb 8th, 2022 (edited)
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1. def spiralPal(m, n, a):
  2.     k = 0
  3.     l = 0
  4.    
  5.     outarr=[]
  6.    
  7.     checkpal=""
  8.    
  9.     while (k < m and l < n):
  10.        
  11.         for i in range(l, n):
  12.             checkpal+=a[k][i]
  13.            
  14.             if len(checkpal)>1 and checkpal==checkpal[::-1]:
  15.                 outarr.append(checkpal)
  16.                 checkpal=""
  17.        
  18.         k += 1
  19.        
  20.         for i in range(k, m):
  21.             checkpal+=a[i][n - 1]
  22.            
  23.             if len(checkpal)>1 and checkpal==checkpal[::-1]:
  24.                 outarr.append(checkpal)
  25.                 checkpal=""
  26.            
  27.         n -= 1
  28.        
  29.         if (k < m):
  30.            
  31.             for i in range(n - 1, (l - 1), -1):
  32.                 checkpal+=a[m - 1][i]
  33.                
  34.                 if len(checkpal)>1 and checkpal==checkpal[::-1]:
  35.                     outarr.append(checkpal)
  36.                     checkpal=""
  37.                
  38.             m -= 1
  39.            
  40.         if (l < n):
  41.            
  42.             for i in range(m - 1, k - 1, -1):
  43.                
  44.                 checkpal+=a[i][l]
  45.                
  46.                 if len(checkpal)>1 and checkpal==checkpal[::-1]:
  47.                     outarr.append(checkpal)
  48.                     checkpal=""
  49.                
  50.             l += 1
  51.    
  52.     if len(checkpal)!=0:
  53.         outarr.append(checkpal)
  54.    
  55.     print(*outarr,sep=",")
  56.  
  57.  
  58. m=int(input())
  59. inmatrix=[]
  60. for i in range(m):
  61.     inmatrix.append(list(input().split(",")))
  62.    
  63.  
  64. R = m
  65. C = len(inmatrix[0])
  66.  
  67. spiralPal(R, C, inmatrix)
  68.  
  69.  
Add Comment
Please, Sign In to add comment