Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,53 @@
|
|||
local fmt = require "fmt"
|
||||
require "table2"
|
||||
|
||||
local function encode(s)
|
||||
if s == "" then return {} end
|
||||
local symbols = "abcdefghijklmnopqrstuvwxyz":split("")
|
||||
local result = table.rep(#s, 0)
|
||||
for i = 1, #s do
|
||||
local c = s[i]
|
||||
local index = symbols:findindex(|ch| -> ch == c)
|
||||
if !index then error($"{s} contains a non-alphabetic character") end
|
||||
result[i] = index
|
||||
if index > 1 then
|
||||
for k = index - 1, 1, -1 do symbols[k + 1] = symbols[k] end
|
||||
symbols[1] = c
|
||||
end
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
local function decode(a)
|
||||
if #a == 0 then return "" end
|
||||
local symbols = "abcdefghijklmnopqrstuvwxyz":split("")
|
||||
local result = table.rep(#a, "")
|
||||
local i = 1
|
||||
for a as n do
|
||||
if n < 1 or n > 26 then error($"{fmt.format("%,s", a)} contains an invalid number") end
|
||||
result[i] = symbols[n]
|
||||
if n > 1 then
|
||||
for j = n - 1, 1, -1 do symbols[j + 1] = symbols[j] end
|
||||
symbols[1] = result[i]
|
||||
end
|
||||
i += 1
|
||||
end
|
||||
return result:concat("")
|
||||
end
|
||||
|
||||
local strings = {"broood", "bananaaa", "hiphophiphop"}
|
||||
local encoded = table.create(#strings)
|
||||
|
||||
for i, s in strings do
|
||||
encoded[i] = encode(s)
|
||||
fmt.print("%-12s -> %,s", s, encoded[i])
|
||||
end
|
||||
print()
|
||||
local decoded = table.create(#encoded)
|
||||
i = 1
|
||||
for encoded as a do
|
||||
decoded[i] = decode(a)
|
||||
local correct = (decoded[i] == strings[i]) ? "correct" : "incorrect"
|
||||
fmt.print("%,-38s -> %-12s -> %s", a, decoded[i], correct)
|
||||
i += 1
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue