|
1 | 1 | --Burrows-Wheeler Transform |
2 | 2 |
|
3 | | -function encode(word) |
4 | | -local t={word} |
5 | | -local pos = 1 |
| 3 | +--Burrows-Wheeler Transform |
6 | 4 |
|
7 | | -local cat = string.len(word) |
8 | | - for i=cat-1,1,-1 do |
9 | | - local tmp = string.sub(word,1,i) |
10 | | - local comp = string.sub(word,i+1,cat) |
11 | | - local str = comp..tmp |
12 | 5 |
|
13 | | - table.insert(t,str) |
14 | | - end |
15 | | -table.sort(t) |
| 6 | +function encode(word) |
| 7 | + local t={word} |
| 8 | + local pos = 1 |
| 9 | + local cat = string.len(word) |
| 10 | + |
| 11 | + for i=cat-1,1,-1 do |
| 12 | + local tmp = string.sub(word,1,i) |
| 13 | + local comp = string.sub(word,i+1,cat) |
| 14 | + local str = comp..tmp |
| 15 | + table.insert(t,str) |
| 16 | + end |
| 17 | + |
| 18 | + table.sort(t) |
16 | 19 |
|
17 | | -local retStr = "" |
| 20 | + local retStr = "" |
18 | 21 | for k in ipairs(t) do |
19 | 22 | if t[k]==word then pos = k end |
20 | 23 | retStr = retStr..string.sub(t[k],string.len(t[k]),string.len(t[k])) |
21 | 24 | end |
22 | 25 |
|
23 | | -return retStr |
| 26 | + return retStr |
24 | 27 | end |
| 28 | + |
| 29 | + |
| 30 | +function decode(encword) |
| 31 | + --print('decode',encword) |
| 32 | + local size = string.len(encword) |
| 33 | + local offset = tonumber(string.sub(encword,1,1)) |
| 34 | + local encword = string.sub(encword,2,size) |
| 35 | + |
| 36 | + local t={} |
| 37 | + for n=0,string.len(encword),1 do |
| 38 | + for i=1,string.len(encword),1 do |
| 39 | + t[i] = not t[i] and '' or string.sub(encword,i,i)..t[i] |
| 40 | + end |
| 41 | + table.sort(t) |
| 42 | + end |
| 43 | + |
| 44 | + return (t[offset]) |
| 45 | +end |
| 46 | + |
| 47 | +--Sample Test |
| 48 | +local str = "SIX.MIXED.PIXIES.SIFT.SIXTY.PIXIE.DUST.BOXES" |
| 49 | +local out = '' |
| 50 | + --for w in string.gfind(str, "%w+") do |
| 51 | + local enc = encode(str) |
| 52 | + local dec = decode(enc) |
| 53 | + out = out .. '.'..enc |
| 54 | + print("\nChaine originale: "..str) |
| 55 | + print("Transformée B-W: "..enc) |
| 56 | + print("Transformée Décodée: "..dec) |
| 57 | + --end |
| 58 | +print(out) |
0 commit comments