Created
January 25, 2020 14:41
-
-
Save Verina-Armanyous/e98361699693c6135b1fd948c9645301 to your computer and use it in GitHub Desktop.
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def lstSpiral(matrix):\n", | |
"\n", | |
" m = len (matrix) #number of rows\n", | |
" n = len (matrix[0]) #number of columns \n", | |
" \n", | |
" finalLst =[]\n", | |
" \n", | |
" #if the matrix is empty\n", | |
" if n == 0 or m == 0:\n", | |
" return []\n", | |
" \n", | |
" # if the matrix is (1 x n) --> 1 row, n columns\n", | |
" if m == 1:\n", | |
" return matrix [0]\n", | |
" \n", | |
" #if the matrix is (num x 1) --> n rows, 1 column \n", | |
" if n == 1:\n", | |
" finalLst =[y for x in matrix for y in x]\n", | |
" return finalLst\n", | |
" \n", | |
" \n", | |
" while len(matrix)> 0:\n", | |
" \n", | |
" #to add the elements of the first row\n", | |
" for item in matrix[0]: \n", | |
" finalLst.append(item) \n", | |
" del matrix[0]\n", | |
"\n", | |
" #to add the far right elements to the lst\n", | |
" if len(matrix)> 0:\n", | |
" for lst in matrix:\n", | |
" finalLst.append(lst[-1])\n", | |
" del lst[-1]\n", | |
" \n", | |
" # to add the elements of the last row\n", | |
" if len(matrix)> 0:\n", | |
" for item in matrix[-1][::-1]:\n", | |
" finalLst.append(item) \n", | |
" del matrix[-1]\n", | |
" \n", | |
" # to add the far left elements to the lst\n", | |
" if len(matrix)> 0:\n", | |
" for lst in matrix[::-1]:\n", | |
" finalLst.append(lst[0])\n", | |
" del lst[0]\n", | |
" \n", | |
" #to clean the matrix from empty lists that might have resulted from deleting elements \n", | |
" if [] in matrix:\n", | |
" a = matrix.count([])\n", | |
" for lst in range(a):\n", | |
" matrix.remove([]) \n", | |
"\n", | |
" return finalLst" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"test_1 = [[\"a\",\"b\",\"c\",\"d\"],[\"e\",\"f\",\"g\",\"h\"],[\"i\",\"j\",\"k\",\"l\"],\n", | |
" [\"m\",\"n\",\"o\",\"p\"],[\"q\",\"r\",\"s\",\"t\"],[\"u\",\"v\",\"w\",\"x\"]]\n", | |
"test_2 = [[]]\n", | |
"test_3 = [[1],[6],[5]]\n", | |
"test_4 = [[\"a\",\"b\",\"c\",\"d\",5,9],\n", | |
" [\"e\",\"f\",\"g\",\"h\",4,0],\n", | |
" [\"i\",\"j\",\"k\",\"l\",3,1]]\n", | |
"assert(lstSpiral(test_1))==['a', 'b', 'c', 'd', 'h', 'l', 'p', 't', 'x', 'w', 'v', 'u', 'q', 'm',\n", | |
" 'i', 'e', 'f', 'g', 'k', 'o', 's', 'r', 'n', 'j']\n", | |
"assert(lstSpiral(test_2))==[]\n", | |
"assert(lstSpiral(test_3))==[1, 6, 5]\n", | |
"assert(lstSpiral(test_4))== ['a', 'b', 'c', 'd', 5, 9, 0, 1, 3, 'l', 'k', 'j', 'i', 'e', \n", | |
" 'f', 'g', 'h', 4]\n" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.4" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment