Python implementation of a cis-clock back-form matrix

Source: Internet
Author: User

Inadvertently saw an interview on the Internet The question is, write a back-shape matrix. The effect of the implementation is broadly as follows:

[1, 2, 3, 4, 5]
[16, 17, 18, 19, 6]
[15, 24, 25, 20, 7]
[14, 23, 22, 21, 8]
[13, 12, 11, 10, 9]

Because the daily use of Python, just beginning to think that the use of the list of the Append method can be done, the hands of the discovery or not. Later, I think we should make a list based on the parameter capacity and replace it with the list replacement method. In the process of processing, still found that there are a lot of thinking off the place.

Finally, on someone else's blog to see the 2004, someone used Python to achieve the inverse of the clock direction of the back-shape matrix. Draw on his method. The general idea is this:

No. 0 Step: What is the characteristic of the back-shape matrix?

cis-clock matrix, in the case of a linear value of 1, in the case of a back shape as a layer:

The "End value"-"Start value" = "Matrix Edge Length"-1, the end value of the fourth matrix Edge is the starting value of the first three matrices.

[1, 2, 3, 4, 5]

[16,,,, 6]

[15,,,, 7]

[14,,,, 8]

[13,12,11,10,9]

  

The first step is to create a two-dimensional array based on the length of the set matrix (as described in the C language, Python is a list of nested lists)

1  for inch Range (size): 2         Arry.append (range (size))

The second step is to Chinesen the data pool into this layer according to the matrix:

1  while size >0:2    Create pool ()3     layer + = 1   4      Size-= 2     

Final code:

  

1 #!/usr/bin/env python2 #Coding:utf-83 4 5 defDraw_matrix (begin, size, layer, Arry, controlle_num):6     #set up the increment matrix in the clockwise direction, according to the hierarchy7     #based on the characteristics of increment 1, the current layer is built up and down, four lists, forming a resource pool8     #the length of each direction list is equal to the length of size9     #[A]Ten     #[8,, 4] One     #[7,6,5] Atop = range (begin, begin+size) -right = Range (begin+size-1, begin+size*2-1) -Bottom = Range (begin+size*2-2, begin+size*3-2) theleft = Range (begin+size*3-3, begin+size*4-3) -LEFT[SIZE-1] = Begin#the last value of the left list of the Shun clock is changed to a start value -  -     #size is the length of the edge of the matrix, I can represent either long or wide +     #I step in to get the values from the pool of resources in this layer -      forIinchRange (size): +Arry[layer][layer+i] =Top[i] AARRY[LAYER+I][CONTROLLE_NUM-LAYER-1] =Right[i] atARRY[CONTROLLE_NUM-LAYER-1][CONTROLLE_NUM-LAYER-I-1] =Bottom[i] -Arry[controlle_num-1-layer-i][layer] =Left[i] -     returnArry -  -  - defMatrix (Size, begin=1, layer=0): inControlle_num =size -Arry = [] to      forIinchRange (size): + arry.append (range (size)) -      whileSize >0: theArry =Draw_matrix (begin, size, layer, Arry, controlle_num) *Begin = begin+ (size-1)) $Size = Size-2Panax NotoginsengLayer = layer + 1 -     returnArry the  +  A if __name__=='__main__': theDAT = Matrix (5) +      forIinchRange (5): -         PrintDat[i]

Python implementation of a cis-clock back-form matrix

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.