Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.59 KB | None | 0 0
  1. def gen_matrix(matrix, pos, s):
  2. '''
  3. in: a matrix (5x5), a position in which you will start a spiral, a letter (s) that if == "r" will build you a spiral in a clockwise way, or, if == "c" will build a spiral in an anticlock way
  4. des: creat a 5x5 full of zeros matrix and separate the nested lists of the matrix. Then, get a pointer to the already separated list (from the matrix) and occupie the position of the 5x5 full of zeros matrix with the nested[pointer] element
  5. out: spiral matrix, clock or anticlock wise
  6. '''
  7. nested=[item for sublist in matrix for item in sublist]
  8. x,y = 5,5 #for this case Change this is u are to change the matrix size
  9. table = [[0]*y for dummy in range(x)] #5x5 full of 0 matrix
  10.  
  11. directions_clock = {"right":[lambda row, column:(row, column+1), lambda row, column:(row+1, column), lambda row, column:(row, column-1),lambda row, column:(row-1, column),], "down": [lambda row, column:(row+1, column),lambda row, column:(row, column-1),lambda row, column:(row-1, column),lambda row, column:(row, column+1)], "left": [lambda row, column:(row, column-1), lambda row, column:(row-1, column), lambda row, column:(row, column+1),lambda row, column:(row+1, column)], "up":[lambda row, column:(row-1, column), lambda row, column:(row, column+1), lambda row, column:(row-1, column),lambda row, column:(row, column-1)]}
  12.  
  13. directions_anti = {"right":[lambda row, column:(row, column+1), lambda row, column:(row-1, column), lambda row, column:(row, column-1),lambda row, column:(row+1, column),], "down": [lambda row, column:(row+1, column),lambda row, column:(row, column+1),lambda row, column:(row-1, column),lambda row, column:(row, column-1)], "left": [lambda row, column:(row, column-1), lambda row, column:(row+1, column), lambda row, column:(row, column+1),lambda row, column:(row-1, column)], "up":[lambda row, column:(row-1, column), lambda row, column:(row, column-1), lambda row, column:(row+1, column),lambda row, column:(row, column+1)]}
  14.  
  15. if s == "r": #clockwise matrix
  16. if pos == faz_pos(0,0):
  17. order_x = directions_clock["right"]
  18. order=iter(order_x)
  19. next_cell = next(order) #first we move left
  20. row, column = linha_pos(pos),coluna_pos(pos)
  21. for i in range(0,x*y+1):
  22. table[row][column] = nested[i]
  23. _row,_column = next_cell(row,column)
  24. # if new column would be (index out of bounds)
  25. # or number is already set
  26. if _column > y-1 or table[_row][_column]:
  27. next_cell = next(order)
  28. row,column = next_cell(row,column)
  29. else:
  30. row,column = _row,_column
  31. return table
  32. elif pos == faz_pos(0,4):
  33. order_x = directions_clock["down"]
  34. order=iter(order_x)
  35. next_cell = next(order) #first we move left
  36. row, column = linha_pos(pos),coluna_pos(pos)
  37. for i in range(0,x*y):
  38. table[row][column] = nested[i]
  39. _row,_column = next_cell(row,column)
  40. # if new column would be (index out of bounds)
  41. # or number is already set
  42. if _column > y-1 or table[_row][_column]:
  43. next_cell = next(order)
  44. row,column = next_cell(row,column)
  45. else:
  46. row,column = _row,_column
  47. return table
  48. elif pos == faz_pos(4,0):
  49. order_x = directions_clock["up"]
  50. order=iter(order_x)
  51. next_cell = next(order) #first we move left
  52. row, column = linha_pos(pos),coluna_pos(pos)
  53. for i in range(0,x*y):
  54. table[row][column] = nested[i]
  55. _row,_column = next_cell(row,column)
  56. # if new column would be (index out of bounds)
  57. # or number is already set
  58. if _column > y-1 or table[_row][_column]:
  59. next_cell = next(order)
  60. row,column = next_cell(row,column)
  61. else:
  62. row,column = _row,_column
  63. return table
  64. elif pos == faz_pos(4,4):
  65. order_x = directions_clock["left"]
  66. order=iter(order_x)
  67. next_cell = next(order) #first we move left
  68. row, column = linha_pos(pos),coluna_pos(pos)
  69. for i in range(0,x*y):
  70. table[row][column] = nested[i]
  71. _row,_column = next_cell(row,column)
  72. # if new column would be (index out of bounds)
  73. # or number is already set
  74. if _column > y-1 or table[_row][_column]:
  75. next_cell = next(order)
  76. row,column = next_cell(row,column)
  77. else:
  78. row,column = _row,_column
  79. return table
  80. elif s == "c":
  81. if pos == faz_pos(0,0):
  82. order_x = directions_anti["down"]
  83. order=iter(order_x)
  84. next_cell = next(order) #first we move left
  85. row, column = linha_pos(pos),coluna_pos(pos)
  86. for i in range(0,x*y):
  87. table[row][column] = nested[i]
  88. _row,_column = next_cell(row,column)
  89. # if new column would be (index out of bounds)
  90. # or number is already set
  91. if _column > y-1 or table[_row][_column]:
  92. next_cell = next(order)
  93. row,column = next_cell(row,column)
  94. else:
  95. row,column = _row,_column
  96. return table
  97. elif pos == faz_pos(0,4):
  98. order_x = directions_anti["down"]
  99. order=iter(order_x)
  100. next_cell = next(order) #first we move left
  101. row, column = linha_pos(pos),coluna_pos(pos)
  102. for i in range(0,x*y):
  103. table[row][column] = nested[i]
  104. _row,_column = next_cell(row,column)
  105. # if new column would be (index out of bounds)
  106. # or number is already set
  107. if _column > y-1 or table[_row][_column]:
  108. next_cell = next(order)
  109. row,column = next_cell(row,column)
  110. else:
  111. row,column = _row,_column
  112. return table
  113. elif pos == faz_pos(4,0):
  114. order_x= directions_anti["up"]
  115. order=iter(order_x)
  116. next_cell = next(order) #first we move left
  117. row, column = linha_pos(pos),coluna_pos(pos)
  118. for i in range(0,x*y):
  119. table[row][column] = nested[i]
  120. _row,_column = next_cell(row,column)
  121. # if new column would be (index out of bounds)
  122. # or number is already set
  123. if _column > y-1 or table[_row][_column]:
  124. next_cell = next(order)
  125. row,column = next_cell(row,column)
  126. else:
  127. row,column = _row,_column
  128. return table
  129. elif pos == faz_pos(4,4):
  130. order_x= directions_anti["left"]
  131. order=iter(order_x)
  132. next_cell = next(order) #first we move left
  133. row, column = linha_pos(pos),coluna_pos(pos)
  134. for i in range(0,x*y):
  135. table[row][column] = nested[i]
  136. _row,_column = next_cell(row,column)
  137. # if new column would be (index out of bounds)
  138. # or number is already set
  139. if _column > y-1 or table[_row][_column]:
  140. next_cell = next(order)
  141. row,column = next_cell(row,column)
  142. else:
  143. row,column = _row,_column
  144. return table
  145.  
  146.  
  147. def gera_chave_espiral(l,mgc,s,pos): #TOOO HARDCODED
  148. '''
  149. in: l, mgc,s,pos l and mgc were described in gera_chave_linhas and s and pos were described in gen_matrix
  150. do: call gera_chave_linhas(l, mgc), build a 5x5 matrix and call gen_matrix to build the spiral
  151. out: spiral form matrix
  152. '''
  153. alphabet = ("A", "B", "C", 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X','Y', 'Z')
  154. if not (isinstance(l, tuple) and len(l) == 25 and e_pos(pos) and isinstance(mgc,str)):
  155. raise ValueError("gera_chave_espiral: argumentos errados")
  156. while pos not in [(0,0), (0,4), (4,0), (4,4)]:
  157. raise ValueError("gera_chave_espiral: argumentos errados")
  158. mx1=gera_chave_linhas(l,mgc)
  159. the_table = gen_matrix(mx1, pos, s)
  160. return the_table
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement