codewar-4kyuSnail 待学习

Posted 布丁的菜园子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codewar-4kyuSnail 待学习相关的知识,希望对你有一定的参考价值。

##题目描述

Description:

Snail Sort

Given an n x n array, return the array elements arranged from outermost elements to the middle element, traveling clockwise.

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 will illustrate things more clearly:

 

NOTE: The idea is not sort the elements from the lowest value to the highest; the idea is to traverse the 2-d array in a clockwise snailshell pattern.

NOTE 2: The 0x0 (empty matrix) is represented as [[]]我好像最后还是没考虑空矩阵的处理。。。啊啊啊啊,路漫漫啊

##思路分析

突然给我来了道4kyu....写了半天写了一长串,结果答案里别人一句就写完了。。。一句。。我的内心是崩溃的

##代码解析

Python,先贴别人的以便学习,第一个方案真是巧妙啊,服。每次递归取第一行,通过zip(*array[1:])将剩下的行列转换,通过[::-1]倒序输出为snail的argument,就又可以取第一行了!

方案3思想和方案1一致,但是表达得直观,更适合我等菜鸟学习。

1 #方案1
2 def snail(array):
3     return list(array[0]) + snail(zip(*array[1:])[::-1]) if array else []
 1 #方案2
 2 def snail(array):
 3     ret = []
 4     if array and array[0]:
 5         size = len(array)
 6         for n in xrange((size + 1) // 2):
 7             for x in xrange(n, size - n):
 8                 ret.append(array[n][x])
 9             for y in xrange(1 + n, size - n):
10                 ret.append(array[y][-1 - n])
11             for x in xrange(2 + n, size - n + 1):
12                 ret.append(array[-1 - n][-x])
13             for y in xrange(2 + n, size - n):
14                 ret.append(array[-y][n])
15     return ret

#
方案3 def snail(array): a = [] while array: a.extend(list(array.pop(0))) array = zip(*array) array.reverse() return a

方案3注意 extend,非 append。extend 的参数是 list.

下面是我自己的。。。完全没用到 python 高效的东西,只用了最最基本的函数,太弱智了

 1 #为方便本地调试,加了很多 print,提交的时候需要注释掉
 2 def snail(array):
 3     ans = []
 4     if len(array) == 1: # preven "index out of range"!!!
 5         ans = array[0]
 6         #print ans
 7         return ans
 8     len_row = len(array) - 1
 9     len_col = len(array[1]) - 1
10     all = (len_row + 1) * (len_col +1 )
11     #print len_col
12     over_row = 0
13     over_col = 0
14     while over_col <= len_col/2:
15         row = over_row  # current row
16         col = over_col
17         while over_col <= col < len_col - over_col:
18             now = array[row][col]
19             ans.append(now)
20             col += 1
21             #print \'now == \', now
22         #print \'this row over, col ==\', col
23 
24         while over_row <= row < len_row - over_row :
25             now = array[row][col]
26             ans.append(now)
27             row += 1
28             #print \'now == \', now
29         #print \'this col over, row ==\', row
30 
31         while over_col < col <= len_col - over_col:
32             now = array[row][col]
33             ans.append(now)
34             col -= 1
35             #print \'now == \', now
36         #print \'this reverse row over, col ==\', col
37 
38         while over_row < row <= len_row - over_row:
39             now = array[row][col]
40             ans.append(now)
41             row -= 1
42             #print \'now == \', now
43     #print \'this reverse col over, col ==\', row
44 
45         over_row += 1
46         over_col += 1
47         #print \'print over_row == \', over_row, \'print over_col == \', over_col
48     if len(ans) < all:
49         last = int(len_row/2)
50         ans.append(array[last][last])
51     #print ans
52     return ans

 

以上是关于codewar-4kyuSnail 待学习的主要内容,如果未能解决你的问题,请参考以下文章

java SpringRetry学习的代码片段

python 机器学习有用的代码片段

学习笔记:python3,代码片段(2017)

学习 PyQt5。在我的代码片段中找不到错误 [关闭]

快速排序-递归实现

面向接口编程 学习 待整理