Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def faz_pos(l,c): #ss
- if not (isinstance(l, int) and l >= 0):
- raise ValueError("faz_pos: argumentos errados")
- if not (isinstance(c, int) and c >= 0):
- raise ValueError("faz_pos: argumentos errados")
- return (l,c)
- #seletores
- def linha_pos(p): #s
- return p[0]
- def coluna_pos(p): # s
- return p[1]
- #Reconhecedores
- def e_pos(arg): #s
- return (isinstance(arg, tuple) and len(arg) == 2 and isinstance(linha_pos(arg), int) and linha_pos(arg) >= 0 and \
- isinstance(coluna_pos(arg), int) and coluna_pos(arg) >= 0)
- #Testes
- def pos_iguais(p1,p2): #ss
- return (e_pos(p1) and e_pos(p2) and linha_pos(p1) == linha_pos(p2) and coluna_pos(p1) == coluna_pos(p2))
- #2.2 Tipo Chave
- #def gera_chave_linhas(l, mgc):
- if not(len(l) == 25 and isinstance(l, tuple) and isinstance(mgc, str) and len(mgc)<= 25):
- raise ValueError("gera_chave_linhas: argumentos errados")
- for i in l:
- if not (isinstance(i,str) and i in Lista):
- raise ValueError("gera_chave_linhas: argumentos errados")
- matriz_1=[]
- for e in mgc.upper():
- if e not in matriz_1:
- matriz_1.append(e)
- for e in l:
- if e not in matriz_1:
- matriz_1.append(e)
- #initialize a new list. Is there any elegant way to do that?
- matriz_group=[]
- for e in range(5):
- matriz_group.append('')
- #Break it into 5*5
- matriz_group[0]=matriz_1[0:5]
- matriz_group[1]=matriz_1[5:10]
- matriz_group[2]=matriz_1[10:15]
- matriz_group[3]=matriz_1[15:20]
- matriz_group[4]=matriz_1[20:25]
- return (tuple(matriz_group[0]),tuple(matriz_group[1]),tuple(matriz_group[2]),tuple(matriz_group[3]),tuple(matriz_group[4]))
- def gera_chave_linhas(l, mgc): #done
- Lista = ("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')
- matrix = []
- mgc1 = list(mgc)
- if not(len(l) == 25 and isinstance(l, tuple) and isinstance(mgc, str) and len(mgc)<= 25):
- raise ValueError("gera_chave_linhas: argumentos errados")
- for i in range(l):
- if i not in Lista and isinstance(i,str):
- raise ValueError("gera_chave_linhas: argumentos errados")
- while len(matrix) <= 25:
- for i in mgc:
- if i in l:
- if i not in matrix:
- matrix += i
- for j in l:
- if j not in matrix:
- matrix += j
- return (tuple(matrix[:5]), tuple(matrix[5:10]), tuple(matrix[10:15]), tuple(matrix[15:20]), tuple(matrix[20:25]))
- # POR FAZER
- def gera_chave_espiral(l,mgc,s,pos):
- if not (isinstance(l, tuple) and len(l) == 25 and e_pos(pos)):
- raise ValueError("gera_chave_espiral: argumentos errados")
- if s == "r":
- def generateMatrix(n, pos):
- n = 5
- if n<=0:
- return []
- matrix=[row[:] for row in [[0]*n]*n]
- row_st=pos[0]
- row_ed=n-1
- col_st=pos[1]
- col_ed=n-1
- current=1
- while (True):
- if current>25:
- break
- for c in range (col_st, col_ed+1):
- matrix[row_st][c]=mgc[c]
- current+=1
- row_st+=1
- for r in range (row_st, row_ed+1):
- matrix[r][col_ed]=mgc[r]
- current+=1
- col_ed-=1
- for c in range (col_ed, col_st-1, -1):
- matrix[row_ed][c]=mgc[c]
- current+=1
- row_ed-=1
- for r in range (row_ed, row_st-1, -1):
- matrix[r][col_st]=mgc[r]
- current+=1
- col_st+=1
- return matrix
- if s == "c":
- return 0
- #Seletor
- def ref_chave(c, p):
- if not (isinstance(p,int) and e_pos(p)):
- raise ValueError("ref_chave: argumentos errados")
- return c[p]
- #Modificador
- def muda_chave(c,p,l):
- Lista = ("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')
- x = p[0]
- y = p[1]
- if not (e_pos(p) and e_chave(c)):
- raise ValueError("muda_chave: argumentos errados")
- if l not in Lista:
- raise ValueError("muda_chave: argumentos errados")
- else:
- cl = list(c)
- clx = list(cl[x])
- clx[y] = l
- if c[:x] == ():
- return (tuple(clx),) + c[x+1:]
- else:
- return c[:x] + (tuple(clx),) + c[x+1:]
- #Reconhecedores
- def e_chave(arg):
- Lista = ("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(arg, tuple) and len(arg) == 5):
- return False
- else:
- for i in arg:
- if not (isinstance(i, tuple) and len(i) == 5):
- return False
- else:
- for j in i:
- if j not in Lista and isinstance(j, str):
- return False
- return True
- #Transformadores
- def string_chave(c):
- if not e_chave(c):
- raise ValueError("string_chave: argumentos errados")
- print(str(c[:5],"\n",c[5:10],'\n',c[10:15],"\n",c[15:20],"\n",c[20:25],"\n"))
- #Funcoes a desenvolver
- def figura(digrm, chave):
- primeiro = chave[0]
- segundo = chave[1]
- primeiroPosition = digrm.grid.find(primeiro)
- segundoPosition = digrm.grid.find(segundo)
- primeiroCoordinates = (primeiroPosition % 5, primeiroPosition / 5)
- segundoCoordinates = (segundoPosition % 5, segundoPosition / 5)
- if digrm == "l" and primeiroCoordinates[0] == segundoCoordinates[0]: # letters are in the same column
- primeiroLetter = digrm.grid[(((primeiroCoordinates[1] - 1) % 5) * 5) + primeiroCoordinates[0]]
- segundoLetter = digrm.grid[(((segundoCoordinates[1] - 1) % 5) * 5) + segundoCoordinates[0]]
- return ("l", primeiroLetter, segundoLetter)
- elif digrm == "c" and primeiroCoordinates[1] == segundoCoordinates[1]: # letters are in the same row
- primeiroLetter = digrm.grid[(primeiroCoordinates[1] * 5) + ((primeiroCoordinates[0] - 1) % 5)]
- segundoLetter = digrm.grid[(segundoCoordinates[1] * 5) + ((segundoCoordinates[0] - 1) % 5)]
- return ("c", primeiroLetter, segundoLetter)
- else: # letters are not in the same row or column, i.e. they form a rectangle
- primeiroLetter = digrm.grid[(primeiroCoordinates[1] * 5) + segundoCoordinates[0]]
- segundoLetter = digrm.grid[(segundoCoordinates[1] * 5) + primeiroCoordinates[0]]
- return ("r", primeiroLetter, segundoLetter)
- def digramas(mens): #ss
- message = list(mens)
- #Delet space
- for unused in range(len(message)):
- if " " in message:
- message.remove(" ")
- #If both letters are the same, add an "X" after the first letter.
- i=0
- for e in range(int(len(message)/2)):
- if message[i]==message[i+1]:
- message.insert(i+1,'X')
- i=i+2
- if len(message)%2 == 1: #If it is odd digit, add an "X" at the end
- message += "X"
- #Grouping
- i=0
- new=[]
- for x in range(1,int(len(message)/2+1)):
- new+=message[i:i+2]
- i=i+2
- return ''.join(new) #joins the little tuples created during this function
- def codifica_l(pos1, pos2, inc):
- plaintext=[]
- pos1l=list(pos1)
- pos2l=list(pos2)
- if inc == 1: #DONE
- if(pos1l[1] < 4):
- if(pos2l[1] <4):
- pos1l[1],pos2l[1] = pos1l[1]+1,pos2l[1]+1 #give value
- return ((pos1l[0],pos1l[1]),(pos2l[0],pos2l[1]))
- else: #give value
- pos1l[1],pos2l[1] = pos1l[1]+1,0
- return ((pos1l[0],pos1l[1]),(pos2l[0],pos2l[1]))
- else: #give value
- if pos2l[1] <4:
- pos1l[1],pos2l[1] = 0,pos2l[1]+1
- return ((pos1l[0],pos1l[1]),(pos2l[0],pos2l[1]))
- else: #give value
- pos1l[1],pos2l[1] = 0,0
- return ((pos1l[0],pos1l[1]),(pos2l[0],pos2l[1]))
- elif inc == -1: #cahcha
- return output
- def codifica_c(pos1, pos2, inc):
- if inc == 1: #cahcha
- return 0
- elif inc == -1: #cahcha
- return 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement