Advertisement
Guest User

Untitled

a guest
Dec 5th, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.65 KB | None | 0 0
  1. def faz_pos(l,c): #ss
  2. if not (isinstance(l, int) and l >= 0):
  3. raise ValueError("faz_pos: argumentos errados")
  4. if not (isinstance(c, int) and c >= 0):
  5. raise ValueError("faz_pos: argumentos errados")
  6. return (l,c)
  7.  
  8. #seletores
  9. def linha_pos(p): #s
  10. return p[0]
  11. def coluna_pos(p): # s
  12. return p[1]
  13.  
  14. #Reconhecedores
  15. def e_pos(arg): #s
  16. return (isinstance(arg, tuple) and len(arg) == 2 and isinstance(linha_pos(arg), int) and linha_pos(arg) >= 0 and \
  17. isinstance(coluna_pos(arg), int) and coluna_pos(arg) >= 0)
  18. #Testes
  19. def pos_iguais(p1,p2): #ss
  20. return (e_pos(p1) and e_pos(p2) and linha_pos(p1) == linha_pos(p2) and coluna_pos(p1) == coluna_pos(p2))
  21.  
  22. #2.2 Tipo Chave
  23.  
  24. #def gera_chave_linhas(l, mgc):
  25. if not(len(l) == 25 and isinstance(l, tuple) and isinstance(mgc, str) and len(mgc)<= 25):
  26. raise ValueError("gera_chave_linhas: argumentos errados")
  27. for i in l:
  28. if not (isinstance(i,str) and i in Lista):
  29. raise ValueError("gera_chave_linhas: argumentos errados")
  30. matriz_1=[]
  31. for e in mgc.upper():
  32. if e not in matriz_1:
  33. matriz_1.append(e)
  34. for e in l:
  35. if e not in matriz_1:
  36. matriz_1.append(e)
  37. #initialize a new list. Is there any elegant way to do that?
  38. matriz_group=[]
  39. for e in range(5):
  40. matriz_group.append('')
  41. #Break it into 5*5
  42. matriz_group[0]=matriz_1[0:5]
  43. matriz_group[1]=matriz_1[5:10]
  44. matriz_group[2]=matriz_1[10:15]
  45. matriz_group[3]=matriz_1[15:20]
  46. matriz_group[4]=matriz_1[20:25]
  47. return (tuple(matriz_group[0]),tuple(matriz_group[1]),tuple(matriz_group[2]),tuple(matriz_group[3]),tuple(matriz_group[4]))
  48. def gera_chave_linhas(l, mgc): #done
  49. 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')
  50. matrix = []
  51. mgc1 = list(mgc)
  52. if not(len(l) == 25 and isinstance(l, tuple) and isinstance(mgc, str) and len(mgc)<= 25):
  53. raise ValueError("gera_chave_linhas: argumentos errados")
  54. for i in range(l):
  55. if i not in Lista and isinstance(i,str):
  56. raise ValueError("gera_chave_linhas: argumentos errados")
  57. while len(matrix) <= 25:
  58. for i in mgc:
  59. if i in l:
  60. if i not in matrix:
  61. matrix += i
  62. for j in l:
  63. if j not in matrix:
  64. matrix += j
  65. return (tuple(matrix[:5]), tuple(matrix[5:10]), tuple(matrix[10:15]), tuple(matrix[15:20]), tuple(matrix[20:25]))
  66.  
  67.  
  68.  
  69. # POR FAZER
  70. def gera_chave_espiral(l,mgc,s,pos):
  71. if not (isinstance(l, tuple) and len(l) == 25 and e_pos(pos)):
  72. raise ValueError("gera_chave_espiral: argumentos errados")
  73. if s == "r":
  74. def generateMatrix(n, pos):
  75. n = 5
  76. if n<=0:
  77. return []
  78.  
  79. matrix=[row[:] for row in [[0]*n]*n]
  80.  
  81. row_st=pos[0]
  82. row_ed=n-1
  83.  
  84. col_st=pos[1]
  85. col_ed=n-1
  86. current=1
  87.  
  88. while (True):
  89. if current>25:
  90. break
  91. for c in range (col_st, col_ed+1):
  92. matrix[row_st][c]=mgc[c]
  93. current+=1
  94. row_st+=1
  95. for r in range (row_st, row_ed+1):
  96. matrix[r][col_ed]=mgc[r]
  97. current+=1
  98. col_ed-=1
  99. for c in range (col_ed, col_st-1, -1):
  100. matrix[row_ed][c]=mgc[c]
  101. current+=1
  102. row_ed-=1
  103. for r in range (row_ed, row_st-1, -1):
  104. matrix[r][col_st]=mgc[r]
  105. current+=1
  106. col_st+=1
  107. return matrix
  108. if s == "c":
  109. return 0
  110.  
  111.  
  112.  
  113. #Seletor
  114. def ref_chave(c, p):
  115. if not (isinstance(p,int) and e_pos(p)):
  116. raise ValueError("ref_chave: argumentos errados")
  117. return c[p]
  118.  
  119. #Modificador
  120. def muda_chave(c,p,l):
  121. 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')
  122. x = p[0]
  123. y = p[1]
  124. if not (e_pos(p) and e_chave(c)):
  125. raise ValueError("muda_chave: argumentos errados")
  126. if l not in Lista:
  127. raise ValueError("muda_chave: argumentos errados")
  128. else:
  129. cl = list(c)
  130. clx = list(cl[x])
  131. clx[y] = l
  132. if c[:x] == ():
  133. return (tuple(clx),) + c[x+1:]
  134. else:
  135. return c[:x] + (tuple(clx),) + c[x+1:]
  136.  
  137.  
  138. #Reconhecedores
  139. def e_chave(arg):
  140. 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')
  141. if not (isinstance(arg, tuple) and len(arg) == 5):
  142. return False
  143. else:
  144. for i in arg:
  145. if not (isinstance(i, tuple) and len(i) == 5):
  146. return False
  147. else:
  148. for j in i:
  149. if j not in Lista and isinstance(j, str):
  150. return False
  151. return True
  152.  
  153.  
  154.  
  155. #Transformadores
  156. def string_chave(c):
  157. if not e_chave(c):
  158. raise ValueError("string_chave: argumentos errados")
  159. print(str(c[:5],"\n",c[5:10],'\n',c[10:15],"\n",c[15:20],"\n",c[20:25],"\n"))
  160.  
  161. #Funcoes a desenvolver
  162.  
  163. def figura(digrm, chave):
  164. primeiro = chave[0]
  165. segundo = chave[1]
  166.  
  167. primeiroPosition = digrm.grid.find(primeiro)
  168. segundoPosition = digrm.grid.find(segundo)
  169.  
  170. primeiroCoordinates = (primeiroPosition % 5, primeiroPosition / 5)
  171. segundoCoordinates = (segundoPosition % 5, segundoPosition / 5)
  172.  
  173. if digrm == "l" and primeiroCoordinates[0] == segundoCoordinates[0]: # letters are in the same column
  174. primeiroLetter = digrm.grid[(((primeiroCoordinates[1] - 1) % 5) * 5) + primeiroCoordinates[0]]
  175. segundoLetter = digrm.grid[(((segundoCoordinates[1] - 1) % 5) * 5) + segundoCoordinates[0]]
  176. return ("l", primeiroLetter, segundoLetter)
  177. elif digrm == "c" and primeiroCoordinates[1] == segundoCoordinates[1]: # letters are in the same row
  178. primeiroLetter = digrm.grid[(primeiroCoordinates[1] * 5) + ((primeiroCoordinates[0] - 1) % 5)]
  179. segundoLetter = digrm.grid[(segundoCoordinates[1] * 5) + ((segundoCoordinates[0] - 1) % 5)]
  180. return ("c", primeiroLetter, segundoLetter)
  181. else: # letters are not in the same row or column, i.e. they form a rectangle
  182. primeiroLetter = digrm.grid[(primeiroCoordinates[1] * 5) + segundoCoordinates[0]]
  183. segundoLetter = digrm.grid[(segundoCoordinates[1] * 5) + primeiroCoordinates[0]]
  184. return ("r", primeiroLetter, segundoLetter)
  185.  
  186.  
  187. def digramas(mens): #ss
  188. message = list(mens)
  189. #Delet space
  190. for unused in range(len(message)):
  191. if " " in message:
  192. message.remove(" ")
  193. #If both letters are the same, add an "X" after the first letter.
  194. i=0
  195. for e in range(int(len(message)/2)):
  196. if message[i]==message[i+1]:
  197. message.insert(i+1,'X')
  198. i=i+2
  199. if len(message)%2 == 1: #If it is odd digit, add an "X" at the end
  200. message += "X"
  201. #Grouping
  202. i=0
  203. new=[]
  204. for x in range(1,int(len(message)/2+1)):
  205. new+=message[i:i+2]
  206. i=i+2
  207. return ''.join(new) #joins the little tuples created during this function
  208.  
  209. def codifica_l(pos1, pos2, inc):
  210. plaintext=[]
  211. pos1l=list(pos1)
  212. pos2l=list(pos2)
  213. if inc == 1: #DONE
  214. if(pos1l[1] < 4):
  215. if(pos2l[1] <4):
  216. pos1l[1],pos2l[1] = pos1l[1]+1,pos2l[1]+1 #give value
  217. return ((pos1l[0],pos1l[1]),(pos2l[0],pos2l[1]))
  218. else: #give value
  219. pos1l[1],pos2l[1] = pos1l[1]+1,0
  220. return ((pos1l[0],pos1l[1]),(pos2l[0],pos2l[1]))
  221. else: #give value
  222. if pos2l[1] <4:
  223. pos1l[1],pos2l[1] = 0,pos2l[1]+1
  224. return ((pos1l[0],pos1l[1]),(pos2l[0],pos2l[1]))
  225. else: #give value
  226. pos1l[1],pos2l[1] = 0,0
  227. return ((pos1l[0],pos1l[1]),(pos2l[0],pos2l[1]))
  228.  
  229. elif inc == -1: #cahcha
  230. return output
  231.  
  232.  
  233. def codifica_c(pos1, pos2, inc):
  234. if inc == 1: #cahcha
  235. return 0
  236. elif inc == -1: #cahcha
  237. return 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement