The algorithm of spiral digit is simple to realize.
Example 5
01 02 03) 04 05
16 17 18) 19 06
15 24 25) 20 07
14 23 22) 21 08
13 12 11) 10 09
By observing, the external numbers wrap around a circle and then collapse inward.
From the program, as long as the recursive processing good 4 edges can be.
At the same time, in order to avoid vertex repetition assignment, the last point allows subsequent edge processing.
Description: Processing is temporarily stored in a list object.
Implementation code:
def getlocindex (l_x,l_y,steps): return l_x + l_y*stepsdef increaseseedandsteps (curseed,cur_steps): RET Urn (curseed +1,cur_steps+1) def settargetitem (targetlst,l_cur_x,l_cur_y,steps,curseed): Loc_index = GetlocIndex (l_cu R_x, l_cur_y, steps) targetlst[loc_index] = Curseeddef Calc (targetlst,seed,l_x,l_y,nextsteps,steps): CU Rrent_seed = Seed Loop_steps = Nextsteps-1 if (Nextsteps < 1): Settargetite M (Targetlst, l_x, L_y,steps, current_seed) return each_steps = 0 while (Each_steps & lt;= loop_steps): Settargetitem (Targetlst, l_x+ Each_steps, L_y,steps, current_seed) current_seed,each_steps = Increaseseedandsteps (Current_seed, EAC h_steps) each_steps = 0 while (each_steps <= loop_steps): Settargetitem (Targetlst, L_x+nextsteps, (L_y+each_steps), steps, current_seed) Current_seed,each_steps = Increaseseedandsteps (current_seed , each_steps) each_steps = 0 while (each_steps <= loop_steps): Settargetitem (Targetlst, L_x+nextsteps-each_steps, l_y+nextsteps, steps, Current_seed) Current_seed,each_steps = Increaseseedandsteps (Current_seed, each_steps) each_steps = 0 while (each_steps <= loop_steps): Settar GetItem (Targetlst, l_x, l_y+nextsteps-each_steps, Steps, Current_seed) Current_seed, Each_steps = Increaseseedandsteps (current_seed, Each_steps) if (nextsteps-2 >= 0): Calc (targetlst,cur Rent_seed,l_x+1,l_y+1,nextsteps-2,steps)
Test code:
def outputresult (targetlst,steps): outbuffer = " for RowIndex in range (0, steps* steps): if (rowIndex% Steps = = 0 and len (outbuffer) >0): print ('%s\n '% (outbuffer)) Outbuffer = ' outbuffer = outbuffer + '%02d '% (Targetlst[rowindex]) print ('%s\n '% (outbuffer)) import tracebacktry: steps =5 targetlst = List ( ) [Targetlst.append (0) for ntry in range (0,steps* steps)] calc (targetlst, 1,0,0,steps-1,steps) Outputresult (Targetlst, steps) except Exception as exc: print ("App catch:%s\n"% (exc)); info = traceback.format_exc () print (info) print ("Done")
Python implementation of spiral numbers