The idea and code example of using the iterator to print the spiral matrix in Python, And the python Matrix

Source: Internet
Author: User

The idea and code example of using the iterator to print the spiral matrix in Python, And the python Matrix

Ideas

A spiral matrix refers to a spiral matrix with numbers increasing from the first line to the right and increasing downward,
To the left, and to the up, so that the cycle.
The spiral matrix is represented by a two-dimensional array. The coordinates (x, y) are (x axis and y axis ).
The clockwise direction of the spiral is-> right, bottom, left, top, which is represented by a numerical value that is x plus 1 lattice (), y plus 1 lattice ), x minus 1 (-1, 0), y minus 1 (0,-1 ).
Coordinates start to walk from (0, 0). When the range is exceeded or the obstacle is encountered, the direction is switched.
To print a spiral matrix, you must first assign values to n * n arrays. According to the rule, each layer is incremented in the order of right-> bottom-> left-> top, therefore, as long as the first number of each layer can be found, the first value is the first number + 4 * n-4 of the previous layer, the cycle of n minus 2 each time.

Code
After the above analysis, the idea is very clear, and the thousand words are not as simple as one code:

Import itertools def spiral (n, m): _ status = itertools. cycle (['right', 'down', 'left', 'up']) # used for periodic state switching _ movemap = {'right ), 'low': (), 'left' :(-), 'up' :( 0,-1),} pos2no = dict. fromkeys ([(x, y) for x in range (n) for y in range (m)]) _ pos = (0, 0) _ st = next (_ status) for I in range (1, n * m + 1): _ oldpos = _ pos = tuple (map (sum, zip (_ pos, _ movemap [_ st]) # Move if (_ pos not in pos2no) or (pos2no [_ pos]) by status: # Switch direction when the range is exceeded or encounters obstacles _ st = next (_ status) _ pos = tuple (map (sum, zip (_ oldpos, _ movemap [_ st]) pos2no [_ oldpos] = I return pos2no def display_spiral (n, m): pos2no = spiral (n, m) for I in range (m): for j in range (n): print pos2no [(j, I)], '\ t ', print '\ n' print'-'* 30 display_spiral (4, 4) display_spiral (5, 4)

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.