螺旋數字的python實現

螺旋數字的算法簡單實現。python

示例 5算法

01 02 03 04 05app

16 17 18 19 06oop

15 24 25 20 07測試

14 23 22 21 08spa

13 12 11 10 09 orm

經過觀察,外部數字進行環繞一圈後向內收攏。對象

num-1

從程序出發,只要遞歸處理好4條邊便可。blog

num-2

同時爲了不頂點重複賦值,最後一個點讓後續的邊處理。遞歸

 

說明:處理暫時存儲在一個list對象中。

實現代碼:

def getlocIndex(l_x,l_y,steps):       
       return l_x  + l_y*steps

def increaseSeedAndSteps(curSeed,cur_steps):
       return (curSeed +1,cur_steps+1)


def setTargetItem(targetlst,l_cur_x,l_cur_y,steps,curSeed):
       loc_index = getlocIndex(l_cur_x, l_cur_y, steps)
       targetlst[loc_index] = curSeed

def calc(targetlst,seed,l_x,l_y,nextsteps,steps):       

       current_seed = seed    
       loop_steps = nextsteps-1

       if( nextsteps < 1 ):              
              setTargetItem(targetlst, l_x, l_y,steps, current_seed)              
              return


       each_steps = 0
       while(each_steps <= loop_steps):                                                              
              setTargetItem(targetlst, l_x+each_steps, l_y,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, (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):                                                        
              setTargetItem(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,current_seed,l_x+1,l_y+1,nextsteps-2,steps)              

  

  

測試代碼:

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 traceback
try:
       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")
相關文章
相關標籤/搜索