Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def gen_matrix(matrix, pos, s):
- '''
- 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
- 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
- out: spiral matrix, clock or anticlock wise
- '''
- nested=[item for sublist in matrix for item in sublist]
- x,y = 5,5 #for this case Change this is u are to change the matrix size
- table = [[0]*y for dummy in range(x)] #5x5 full of 0 matrix
- 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)]}
- 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)]}
- if s == "r": #clockwise matrix
- if pos == faz_pos(0,0):
- order_x = directions_clock["right"]
- order=iter(order_x)
- next_cell = next(order) #first we move left
- row, column = linha_pos(pos),coluna_pos(pos)
- for i in range(0,x*y+1):
- table[row][column] = nested[i]
- _row,_column = next_cell(row,column)
- # if new column would be (index out of bounds)
- # or number is already set
- if _column > y-1 or table[_row][_column]:
- next_cell = next(order)
- row,column = next_cell(row,column)
- else:
- row,column = _row,_column
- return table
- elif pos == faz_pos(0,4):
- order_x = directions_clock["down"]
- order=iter(order_x)
- next_cell = next(order) #first we move left
- row, column = linha_pos(pos),coluna_pos(pos)
- for i in range(0,x*y):
- table[row][column] = nested[i]
- _row,_column = next_cell(row,column)
- # if new column would be (index out of bounds)
- # or number is already set
- if _column > y-1 or table[_row][_column]:
- next_cell = next(order)
- row,column = next_cell(row,column)
- else:
- row,column = _row,_column
- return table
- elif pos == faz_pos(4,0):
- order_x = directions_clock["up"]
- order=iter(order_x)
- next_cell = next(order) #first we move left
- row, column = linha_pos(pos),coluna_pos(pos)
- for i in range(0,x*y):
- table[row][column] = nested[i]
- _row,_column = next_cell(row,column)
- # if new column would be (index out of bounds)
- # or number is already set
- if _column > y-1 or table[_row][_column]:
- next_cell = next(order)
- row,column = next_cell(row,column)
- else:
- row,column = _row,_column
- return table
- elif pos == faz_pos(4,4):
- order_x = directions_clock["left"]
- order=iter(order_x)
- next_cell = next(order) #first we move left
- row, column = linha_pos(pos),coluna_pos(pos)
- for i in range(0,x*y):
- table[row][column] = nested[i]
- _row,_column = next_cell(row,column)
- # if new column would be (index out of bounds)
- # or number is already set
- if _column > y-1 or table[_row][_column]:
- next_cell = next(order)
- row,column = next_cell(row,column)
- else:
- row,column = _row,_column
- return table
- elif s == "c":
- if pos == faz_pos(0,0):
- order_x = directions_anti["down"]
- order=iter(order_x)
- next_cell = next(order) #first we move left
- row, column = linha_pos(pos),coluna_pos(pos)
- for i in range(0,x*y):
- table[row][column] = nested[i]
- _row,_column = next_cell(row,column)
- # if new column would be (index out of bounds)
- # or number is already set
- if _column > y-1 or table[_row][_column]:
- next_cell = next(order)
- row,column = next_cell(row,column)
- else:
- row,column = _row,_column
- return table
- elif pos == faz_pos(0,4):
- order_x = directions_anti["down"]
- order=iter(order_x)
- next_cell = next(order) #first we move left
- row, column = linha_pos(pos),coluna_pos(pos)
- for i in range(0,x*y):
- table[row][column] = nested[i]
- _row,_column = next_cell(row,column)
- # if new column would be (index out of bounds)
- # or number is already set
- if _column > y-1 or table[_row][_column]:
- next_cell = next(order)
- row,column = next_cell(row,column)
- else:
- row,column = _row,_column
- return table
- elif pos == faz_pos(4,0):
- order_x= directions_anti["up"]
- order=iter(order_x)
- next_cell = next(order) #first we move left
- row, column = linha_pos(pos),coluna_pos(pos)
- for i in range(0,x*y):
- table[row][column] = nested[i]
- _row,_column = next_cell(row,column)
- # if new column would be (index out of bounds)
- # or number is already set
- if _column > y-1 or table[_row][_column]:
- next_cell = next(order)
- row,column = next_cell(row,column)
- else:
- row,column = _row,_column
- return table
- elif pos == faz_pos(4,4):
- order_x= directions_anti["left"]
- order=iter(order_x)
- next_cell = next(order) #first we move left
- row, column = linha_pos(pos),coluna_pos(pos)
- for i in range(0,x*y):
- table[row][column] = nested[i]
- _row,_column = next_cell(row,column)
- # if new column would be (index out of bounds)
- # or number is already set
- if _column > y-1 or table[_row][_column]:
- next_cell = next(order)
- row,column = next_cell(row,column)
- else:
- row,column = _row,_column
- return table
- def gera_chave_espiral(l,mgc,s,pos): #TOOO HARDCODED
- '''
- in: l, mgc,s,pos l and mgc were described in gera_chave_linhas and s and pos were described in gen_matrix
- do: call gera_chave_linhas(l, mgc), build a 5x5 matrix and call gen_matrix to build the spiral
- out: spiral form matrix
- '''
- 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')
- if not (isinstance(l, tuple) and len(l) == 25 and e_pos(pos) and isinstance(mgc,str)):
- raise ValueError("gera_chave_espiral: argumentos errados")
- while pos not in [(0,0), (0,4), (4,0), (4,4)]:
- raise ValueError("gera_chave_espiral: argumentos errados")
- mx1=gera_chave_linhas(l,mgc)
- the_table = gen_matrix(mx1, pos, s)
- return the_table
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement