# #题目描述Description: Snail Sort
Given an n x n array, return the array elements arranged from outermost elements to the middle element, traveling Clockwis E.
array = [[1,2,3], [4,5,6], [7,8,9]]snail(array) #=> [1,2,3,6,9,8,7,4,5]
For better understanding, please follow the numbers of the next array consecutively:
array = [[1,2,3], [8,9,4], [7,6,5]]snail(array) #=> [1,2,3,4,5,6,7,8,9]
This image would illustrate things more clearly:
Note:the idea isn't sort the elements from the lowest value to the highest; The idea was to traverse the 2-d array in a clockwise Snailshell pattern.
NOTE 2:the 0x0 (empty matrix) is represented as[[]]我好像最后还是没考虑空矩阵的处理。。。啊啊啊啊,路漫漫啊
1 helper = Paginationhelper ([' A ', ' B ', ' C ', ' d ', ' e ', ' F '], 4) 2 Helper.page_count # should = = 2 3 Helper.item_count # should = = 6 4 helper.page_item_count (0) # should = 4 5 helper.page_item_count (1) # Last page-should = 2 6 Helper.page_item _count (2) # should = = 1 Since the page is invalid 7 8 # Page_ndex takes a item index and returns the page that it was Longs on 9 Helper.page_index (5) # should = 1 (zero based index) Helper.page_index (2) # should = = 011 Helper.page_index ( # should = = -112 Helper.page_index ( -10) # should = =-1 Because negative indexes is invalid
# #思路分析
Suddenly I came to the road 4kyu .... Wrote a long string of writing, the result of the answer in the other people wrote a sentence ... A sentence. My heart is broken.
# #代码解析
Python, first put someone else to learn.
1 # Scenario 1 2 def snail (array): 3 return if Else []
1 #Scenario 22 defsnail (array):3RET = []4 ifArray andArray[0]:5Size =Len (Array)6 forNinchXrange ((size + 1)//2):7 forXinchXrange (n, Size-N):8 ret.append (array[n][x])9 forYinchXrange (1 + N, size-N):TenRet.append (Array[y][-1-N]) One forXinchXrange (2 + N, size-n + 1): ARet.append (array[-1-n][-x]) - forYinchXrange (2 + N, size-N): -Ret.append (array[-y] [n]) the returnRet
# Scenario 3 def snail (Array): = [] while array: a.extend (List (array.pop (0))) = Zip (*array) array.reverse () return A
Here is my own ... Completely useless to python efficient things, only with the most basic function, too retarded
1 #To facilitate local debugging, add a lot of print, submit the time need to comment out2 defsnail (array):3Ans = []4 ifLen (Array) = = 1:#Preven "index out of range"!!!5Ans =Array[0]6 #Print ans7 returnans8Len_row = Len (array)-19Len_col = Len (array[1])-1TenAll = (Len_row + 1) * (Len_col +1 ) One #Print Len_col AOver_row =0 -Over_col =0 - whileOver_col <= LEN_COL/2: therow = Over_row#Current Row -Col =Over_col - whileOver_col <= Col < len_col-Over_col: -now =Array[row][col] + Ans.append (now) -Col + = 1 + #print ' now = = ', now A #print ' This row over, col = = ', col at - whileOver_row <= Row < Len_row-Over_row: -now =Array[row][col] - Ans.append (now) -Row + = 1 - #print ' now = = ', now in #print ' This col-over, row = = ', row - to whileOver_col < col <= Len_col-Over_col: +now =Array[row][col] - Ans.append (now) theCol-= 1 * #print ' now = = ', now $ #print ' This reverse row over, col = = ', colPanax Notoginseng - whileOver_row < row <= Len_row-Over_row: thenow =Array[row][col] + Ans.append (now) ARow-= 1 the #print ' now = = ', now + #print ' This reverse col-over, col = = ', row - $Over_row + = 1 $Over_col + = 1 - #print ' Print Over_row = = ', Over_row, ' print over_col = = ', Over_col - ifLen (ANS) <All : thelast = Int (LEN_ROW/2) - ans.append (Array[last][last])Wuyi #Print ans the returnAns
"Codewar-4kyu" Snail to learn