Guest User

Untitled

a guest
Jun 18th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. import math
  2.  
  3. letras = "abcdefghijklmnopqrstuvxwyz"
  4.  
  5. arquivo = open ('to be or not be.txt','r')
  6.  
  7. contador = range(0, len(letras))
  8. contador = map(lambda x: 0, contador)
  9. estatistica = contador
  10.  
  11. texto = ""
  12. for linha in arquivo:
  13. temp = linha.lower()
  14. texto += temp
  15.  
  16. it = 0
  17. for it in range(len(letras)):
  18. contador[it] += texto.count(letras[it])
  19.  
  20. soma = reduce(lambda x,y:x+y,contador)
  21. it = 0
  22. for it in range(len(letras)):
  23. estatistica[it] = math.floor(contador[it])/soma
  24. #print letras[it],":", estatistica
  25.  
  26. codigo = ('0','10','110','1110','11110','111110','1111110','11111110','111111110','1111111110','11111111110','111111111110','1111111111110','11111111111110','111111111111110','1111111111111110','11111111111111110','111111111111111110','1111111111111111110','11111111111111111110','111111111111111111110','1111111111111111111110','11111111111111111111110','111111111111111111111110','1111111111111111111111110','11111111111111111111111110')
  27.  
  28. i = 0
  29. for i in range(len(letras)):
  30. texto.replace(letras[i],codigo[i])
  31.  
  32. print "Texto normal:", len(texto)
  33.  
  34.  
  35. it = 0
  36. texto_code = texto
  37. maior = estatistica[0]
  38. j = 0
  39. k = 0
  40. for j in range(len(estatistica)):
  41. it = 0
  42. k = 0
  43. for it in range(len(estatistica)):
  44. if estatistica[it] > maior:
  45. maior = estatistica[it]
  46. k = it
  47. texto_code = texto_code.replace(letras[k],codigo[0])
  48. codigo = codigo[1:len(codigo)]
  49. estatistica.remove(estatistica[k])
  50. letras.replace(letras[k],'')
  51. if len(estatistica)>0:
  52. maior = estatistica[0]
  53.  
  54.  
  55.  
  56. print " Numero de caracteres:",soma
  57. print " Texto Codificado:",len(texto_code)
Add Comment
Please, Sign In to add comment