本文主要是介绍Python练习题答案: 顺时针螺旋【难度:3级】--景越Python编程实例训练营,1000道上机题等你来挑战,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
顺时针螺旋【难度:3级】:
答案1:
from collections import dequedef createSpiral(N):if type(N) != int or N < 0: return []ans = [[0]*N for i in range(N)]x, y = 0, 0moves = deque([(0,1), (1,0), (0,-1) , (-1,0)])for i in range(1, N**2+1):ans[x][y] = iif not ( 0 <= x+moves[0][0] < N and 0 <= y+moves[0][1] < N) or ans[ x+moves[0][0] ][ y+moves[0][1] ] != 0:moves.rotate(-1)x, y = x+moves[0][0], y+moves[0][1]return ans
答案2:
def createSpiral(n):if type(n) != int: return []r, c, turns, rows, cols, spiral, = 0, -1, 0, [0, 1, 0, -1], [1, 0, -1, 0], [[0]*n for i in range(n)]for i in range(1, n*n + 1):if r + rows[turns%4] >= n or c + cols[turns%4]>= n or spiral[r+rows[turns%4]][c+cols[turns%4]]:turns += 1r, c = r + rows[turns%4], c + cols[turns%4]spiral[r][c] = ireturn spiral
答案3:
def createSpiral(n):if not isinstance(n, int): return []x, y, i, dx, dy = 0, 0, 1, 1, 0grid = [[0] * n for _ in range(n)]for r in range(n-1, -1, -2):if not r: grid[y][x] = ifor _ in range(4):for _ in range(r):grid[y][x] = i # [y][x] for cw, [x][y] for ccwx += dx; y += dy; i += 1dx, dy = -dy, dxx += 1; y += 1return grid
答案4:
class Table(dict):def __init__(self, N):self.size = Nself.cursor = 0self.direction = 1for x in range(N):self[-1+x*1j] = self[N+x*1j] = -1self[x-1j] = self[x+N*1j] = -1__getitem__ = dict.getdef put(self, num):if self[self.cursor]: returnself[self.cursor] = nummoving = self.cursor + self.directionif self[moving]:self.direction *= 1jmoving = self.cursor + self.directionself.cursor = movingreturn True@propertydef as_list(self):return [ [self[x+y*1j] for x in range(self.size)]for y in range(self.size) ]def createSpiral(N):if not isinstance(N,int) or N<1: return []table = Table(N)num = 1while table.put(num):num += 1return table.as_list
答案5:
def createSpiral(N):if not type(N) is int or N < 1: return []a = [[0 for col in range(N)] for row in range(N)];steps = [0, 1, 0, -1]turn = 0c = 1i = 0j = 0passed = 0original = Nwhile N > 0:a[i][j] = cif (c - passed) % N == 0:passed += Nif (turn % 2) == 0:N-=1turn+=1i += steps[(turn + 4) % 4]j += steps[(turn + 5) % 4]c+=1print(original, '=', a)return a
答案6:
def createSpiral(n):if type(n)!=int:return []m = [[1]*n for i in range(n)]for i in range((n+1)//2):for a in range(i,n-1-i):m[i][a+1] = m[i][a]+1for b in range(i,n-1-i):m[b+1][n-1-i] = m[b][n-1-i]+1for c in range(i,n-1-i):m[n-1-i][n-c-2] = m[n-1-i][n-c-1]+1for d in range(i,n-2-i):m[n-d-2][i] = m[n-d-1][i]+1if i<(n-1)//2:m[i+1][i+1] = m[i][i]+4*(n-2*i)-4return m
答案7:
# right = 0, down = 1, left = 2, up = 3
shift = [(0, 1), (1, 0), (0, -1), (-1, 0)]s = lambda a, b: (a[0]+b[0], a[1]+b[1])
check = lambda x, s: x[0] > -1 and x[0] < s and x[1] > -1 and x[1] < s
def f(field, pos, fill):field[pos] = fillreturn fill + 1def createSpiral(size):if not isinstance(size, int):print('Suka-Blyat')return []field = {(x,y):0 for x in range(size) for y in range(size)}dir = 0pos = (0, 0)fill = 1if check(pos, size):fill = f(field, pos, fill)move = 2while move > 0:check_pos = s(pos, shift[dir])if check(check_pos, size) and field[check_pos] == 0:pos = s(pos, shift[dir])fill = f(field, pos, fill)move = 2else:dir = (dir + 1) % 4move = move - 1return [[field[(x,y)] for y in range(size)] for x in range(size)]
答案8:
def rotate(arr):return map(list, map(reversed, zip(*arr)))def sides(n):lengths = (i // 2 for i in xrange(2, n * 2 + 1))nums = range(n * n, 0, -1)for length in lengths:yield nums[:length]nums = nums[length:]def createSpiral(n):try: n = int(n) except: return []arr = []for side in sides(n):arr.append(side)arr = rotate(arr)arr = rotate(arr)return arr
答案9:
def createSpiral(n):if not isinstance(n, int):return ''m = [[None] * n for _ in range(n)]mov = [(0, 1), (1, 0), (0, -1), (-1, 0)]x, y = 0, -1num = 1for i in range(2 * n - 1):for j in range((2 * n - i) // 2):x += mov[i % 4][0]y += mov[i % 4][1]m[x][y] = numnum += 1return m
答案10:
import numpy as npdef createSpiral(size):if type(size) is not int or size < 1: return ''spiral = np.array([[0 for col in range(size)] for row in range(size)])rotation_count, current = 0, 0while True:depth = rotation_count // 4zeros = spiral[depth].tolist().count(0)zero_index = spiral[depth].tolist().index(0)head = spiral[depth][:spiral[depth].tolist().index(0)].tolist()middle = list(range(current+1, current+zeros+1))tail = spiral[depth][zero_index+zeros:].tolist()spiral[depth] = head + middle + tailspiral = np.rot90(spiral)rotation_count += 1current += zerosif 0 not in spiral:breakwhile rotation_count % 4:spiral = np.rot90(spiral)rotation_count += 1return ''.join(str(row) for row in spiral.tolist())
景越Python基础训练营QQ群
欢迎各位同学加群讨论,一起学习,共同成长!
这篇关于Python练习题答案: 顺时针螺旋【难度:3级】--景越Python编程实例训练营,1000道上机题等你来挑战的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!