#!/usr/bin/env python3 ''' given a letter, print the text spiral of letters to that letter, e.g. G -> G F A B E D C There is a recursive solution of concentric rings: 21 22 23 24 25 20 10 19 11 18 12 17 16 15 14 13 7 8 9 6 2 5 4 3 1 ''' import math, sys def layer(N, nrings): ''' return layer `N' of the spiral with `nrings' concentric rings ''' outer = 2 * nrings - 1 inner = outer - 2 ring = [1] if nrings == 1 else list(range(inner**2+1, outer**2+1)) first = ring[-1*outer : ] left = ring[-1*outer-N] right = ring[N-1] last = ring[inner : inner+outer][::-1] if N == 0: return first elif 0 < N < outer - 1: return [left] + layer(N-1,nrings-1) + [right] else: return last goal = 'G' if len(sys.argv) == 1 else sys.argv[1] nchars = ord(goal) - ord('A') + 1 nrings = math.ceil(math.sqrt(nchars)) nrings = 1 + math.floor( nrings / 2 ) nlayers = 2 * nrings - 1 alpha = lambda x: chr(ord('A')+x-1) if x <= nchars else ' ' for i in range(nlayers): print( ' '.join( alpha(x) for x in layer(i, nrings)) )