Advertisement
Bolodefchoco_LUAXML

[AI] Cifra Rotacionada

Nov 26th, 2016
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.19 KB | None | 0 0
  1. --Creator: Bolodefchoco
  2. --Made in: 26/11/2015
  3. --Last update: 26/11/2016
  4. --[[ Notes:
  5.     Verifica a melhor compatibilidade num texto rotacionado.
  6.     Exemplo: abc = ghi
  7. ]]--
  8.  
  9. text = "x adkt ndj cdl pcs udgtktg" --> Texto rotacionado
  10. text = text:lower()
  11.  
  12. POTENCY = 25 --> Altere a potência de verificação
  13.  
  14. frequency = {
  15.     a =  0.08167,
  16.     b =  0.01492,
  17.     c =  0.02782,
  18.     d =  0.04253,
  19.     e =  0.12702,
  20.     f =  0.02228,
  21.     g =  0.02015,
  22.     h =  0.06094,
  23.     i =  0.06966,
  24.     j =  0.00153,
  25.     k =  0.00772,
  26.     l =  0.04025,
  27.     m =  0.02406,
  28.     n =  0.06749,
  29.     o =  0.07507,
  30.     p =  0.01929,
  31.     q =  0.00095,
  32.     r =  0.05987,
  33.     s =  0.06327,
  34.     t =  0.09056,
  35.     u =  0.02758,
  36.     v =  0.00978,
  37.     w =  0.02360,
  38.     x =  0.00150,
  39.     y =  0.01974,
  40.     z =  0.00074,
  41. }
  42.  
  43. string.rotate = function(str,i)
  44.     local newStr = ""
  45.     for k = 1,#str do
  46.         local b = str:byte(k)
  47.         if b ~= 32 then
  48.             b = 97 + (((b - 97) + i) % 26)
  49.         end
  50.         newStr = newStr .. string.char(b)
  51.     end
  52.     return newStr
  53. end
  54.  
  55.  
  56. string.letterFrequency = function(str)
  57.     local freq = {}
  58.     for i = 1,#str do
  59.         local letter = str:sub(i,i)
  60.         if letter ~= " " then
  61.             freq[letter] = (freq[letter] and freq[letter]+1 or 1)
  62.         end
  63.     end
  64.     for k,v in next,freq do
  65.         freq[k] = v / #str
  66.     end
  67.     return freq
  68. end
  69.  
  70.  
  71. string.compatibility = function(foo)
  72.     local d = 0
  73.     for k,v in next,frequency do
  74.         if foo.lfreq[k] then
  75.             d = d + math.abs(foo.lfreq[k] - v)
  76.         end
  77.     end
  78.     return 1-d
  79. end
  80.  
  81. io.write("Sentence: "..text.."\nPotency: "..POTENCY.."\n")
  82.  
  83. txtformat = "@Rotation: %d\n\t#Sentence: %s\n\t#Compatibility: %.18f%%\n"
  84.  
  85. solve = {}
  86. solution = {
  87.     rotation = 0,
  88.     compatibility = 0
  89. }
  90. for i = 1,POTENCY do
  91.     solve[i] = {}
  92.  
  93.     solve[i].txt = string.rotate(text,i)
  94.  
  95.     solve[i].lfreq = string.letterFrequency(solve[i].txt)
  96.  
  97.     solve[i].compatibility = string.compatibility(solve[i])
  98.  
  99.     if solution.compatibility < solve[i].compatibility then
  100.         io.write("\n$BEAT "..i.."\n")
  101.         solution = {
  102.             rotation = i,
  103.             compatibility = solve[i].compatibility
  104.         }
  105.     end
  106.  
  107.     io.write(txtformat:format(i,solve[i].txt,solve[i].compatibility))
  108. end
  109.  
  110. io.write("\n>> BEST\n\t" .. txtformat:format(solution.rotation,solve[solution.rotation].txt,solution.compatibility))
  111.  
  112. os.execute("pause >nul")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement