python螺旋运行代码,在Python中创建一个螺旋数组?

Me and my mate were trying to create a fun game in python where the elements entered in the array are accessed in a spiral manner. I have tried few methods like one given below (source).

def spiral(X, Y):

x = y = 0

dx = 0

dy = -1

for i in range(max(X, Y)**2):

if (-X/2 < x <= X/2) and (-Y/2 < y <= Y/2):

print (x, y)

# DO STUFF...

if x == y or (x < 0 and x == -y) or (x > 0 and x == 1-y):

dx, dy = -dy, dx

x, y = x+dx, y+dy

The above statement accesses the elements in spiral loop and prints them for a defined array AE. I would like to know how can I transform a given array AE to a spiral one

8d125701c0a37d821fcf501f12b762d7.png

解决方案

You can build a spiral by starting near the center of the matrix and always turning right unless the element has been visited already:

#!/usr/bin/env python

NORTH, S, W, E = (0, -1), (0, 1), (-1, 0), (1, 0) # directions

turn_right = {NORTH: E, E: S, S: W, W: NORTH} # old -> new direction

def spiral(width, height):

if width < 1 or height < 1:

raise ValueError

x, y = width // 2, height // 2 # start near the center

dx, dy = NORTH # initial direction

matrix = [[None] * width for _ in range(height)]

count = 0

while True:

count += 1

matrix[y][x] = count # visit

# try to turn right

new_dx, new_dy = turn_right[dx,dy]

new_x, new_y = x + new_dx, y + new_dy

if (0 <= new_x < width and 0 <= new_y < height and

matrix[new_y][new_x] is None): # can turn right

x, y = new_x, new_y

dx, dy = new_dx, new_dy

else: # try to move straight

x, y = x + dx, y + dy

if not (0 <= x < width and 0 <= y < height):

return matrix # nowhere to go

def print_matrix(matrix):

width = len(str(max(el for row in matrix for el in row if el is not None)))

fmt = "{:0%dd}" % width

for row in matrix:

print(" ".join("_"*width if el is None else fmt.format(el) for el in row))

Example:

>>> print_matrix(spiral(5, 5))

21 22 23 24 25

20 07 08 09 10

19 06 01 02 11

18 05 04 03 12

17 16 15 14 13

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值