Advertisement
BobMe

inprooog thing

Nov 20th, 2019
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.04 KB | None | 0 0
  1. alphabet = {} -- optimized use of an alphabet
  2. alphabet["a"] = "a";alphabet["b"] = "b";alphabet["c"] = "c";alphabet["d"] = "d";alphabet["e"] = "e";alphabet["f"] = "f";alphabet["g"] = "g";alphabet["h"] = "h";alphabet["i"] = "i";alphabet["j"] = "j";alphabet["k"] = "k";alphabet["l"] = "l";alphabet["m"] = "m";alphabet["n"] = "n";alphabet["o"] = "o";alphabet["p"] = "p";alphabet["q"] = "q";alphabet["r"] = "r";alphabet["s"] = "s";alphabet["t"] = "t";alphabet["u"] = "u";alphabet["v"] = "v";alphabet["w"] = "w";alphabet["x"] = "x";alphabet["y"] = "y";alphabet["z"] = "z"
  3.  
  4. input = "Compress this: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
  5. output = ""
  6. last = ""
  7. lastn = 0
  8.  
  9. -- Compression:
  10. print("Input: "..input)
  11. print("Compressing...")
  12. for i=1,#input do
  13. if last == "" then
  14. last = string.sub(input,i,i)
  15. lastn = i
  16. end
  17.  
  18. if last ~= string.sub(input,i,i) or i == #input then
  19. local fricc = string.sub(input,lastn,i-1)
  20. output = output..last..#fricc
  21. last = string.sub(input,i,i)
  22. lastn = i
  23. end
  24. end
  25. print("\nCompressed: "..output)
  26. -- Decompression
  27. function isNumber(x)
  28. local numbers = {}
  29. numbers["1"] = "1";numbers["2"] = "2";numbers["3"] = "3";numbers["4"] = "4";numbers["5"] = "5";numbers["6"] = "6";numbers["7"] = "7";numbers["8"] = "8";numbers["9"] = "9";numbers["0"] = "0"
  30. if numbers[x] ~= nil then
  31. return true
  32. else
  33. return false
  34. end
  35. end
  36.  
  37. function split(x)
  38. local tab = {}
  39. for i=1,#x do
  40. local num = string.sub(x,i,i)
  41. for k=1,#x-i do
  42. num = num.."0"
  43. end
  44. if tonumber(num) ~= 0 then
  45. -- if num ~= "50" and num ~= "5" and num ~= "500" then
  46. local numberofzeros = #num-1
  47. for i=1,tonumber(string.sub(num,1,1)) do
  48. local newnum = 1
  49. for i=1,numberofzeros do
  50. newnum = newnum.."0"
  51. end
  52. table.insert(tab,tonumber(newnum))
  53. end
  54. -- end
  55. end
  56. end
  57. return tab
  58. end
  59.  
  60. --print(kek("125"))
  61.  
  62. local last = 0
  63. local let = ""
  64. local output2 = ""
  65. local num = 0
  66. local don = false
  67. for i=1,#output do
  68. if i == 1 then
  69. last = 1
  70. let = string.sub(output,i,i)
  71. end
  72. if i ~= 1 and isNumber(string.sub(output,i,i)) == true and don == false then
  73. for k=i,#output do
  74. if isNumber(string.sub(output,k,k)) == false or k == #output then
  75. if k == #output then
  76. num = k
  77. else
  78. num = k-1
  79. end
  80. break
  81. end
  82. end
  83. local numm = string.sub(output,i,num)
  84. local numm2 = split(numm)
  85. local numm3 = 0
  86. for i=1,#numm2 do
  87. numm3 = numm3 + numm2[i]
  88. end
  89. for k=1,numm3 do
  90. output2 = output2..let
  91. end
  92. don = true
  93. elseif i ~= 1 and isNumber(string.sub(output,i,i)) == false and don == true then
  94. last = i
  95. let = string.sub(output,i,i)
  96. don = false
  97. end
  98. end
  99. output2 = output2..string.sub(output2,#output2,#output2) -- add the missing piece
  100. print("Decompressed: "..output2)
  101.  
  102. if output2 == input then
  103. print("\n✓ Decompressed and Input remained the same")
  104. else
  105. print("\n✗ Decompressed and Input are different, please look for errors.")
  106. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement