Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- alphabet = {} -- optimized use of an alphabet
- 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"
- input = "Compress this: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
- output = ""
- last = ""
- lastn = 0
- -- Compression:
- print("Input: "..input)
- print("Compressing...")
- for i=1,#input do
- if last == "" then
- last = string.sub(input,i,i)
- lastn = i
- end
- if last ~= string.sub(input,i,i) or i == #input then
- local fricc = string.sub(input,lastn,i-1)
- output = output..last..#fricc
- last = string.sub(input,i,i)
- lastn = i
- end
- end
- print("\nCompressed: "..output)
- -- Decompression
- function isNumber(x)
- local numbers = {}
- 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"
- if numbers[x] ~= nil then
- return true
- else
- return false
- end
- end
- function split(x)
- local tab = {}
- for i=1,#x do
- local num = string.sub(x,i,i)
- for k=1,#x-i do
- num = num.."0"
- end
- if tonumber(num) ~= 0 then
- -- if num ~= "50" and num ~= "5" and num ~= "500" then
- local numberofzeros = #num-1
- for i=1,tonumber(string.sub(num,1,1)) do
- local newnum = 1
- for i=1,numberofzeros do
- newnum = newnum.."0"
- end
- table.insert(tab,tonumber(newnum))
- end
- -- end
- end
- end
- return tab
- end
- --print(kek("125"))
- local last = 0
- local let = ""
- local output2 = ""
- local num = 0
- local don = false
- for i=1,#output do
- if i == 1 then
- last = 1
- let = string.sub(output,i,i)
- end
- if i ~= 1 and isNumber(string.sub(output,i,i)) == true and don == false then
- for k=i,#output do
- if isNumber(string.sub(output,k,k)) == false or k == #output then
- if k == #output then
- num = k
- else
- num = k-1
- end
- break
- end
- end
- local numm = string.sub(output,i,num)
- local numm2 = split(numm)
- local numm3 = 0
- for i=1,#numm2 do
- numm3 = numm3 + numm2[i]
- end
- for k=1,numm3 do
- output2 = output2..let
- end
- don = true
- elseif i ~= 1 and isNumber(string.sub(output,i,i)) == false and don == true then
- last = i
- let = string.sub(output,i,i)
- don = false
- end
- end
- output2 = output2..string.sub(output2,#output2,#output2) -- add the missing piece
- print("Decompressed: "..output2)
- if output2 == input then
- print("\n✓ Decompressed and Input remained the same")
- else
- print("\n✗ Decompressed and Input are different, please look for errors.")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement