2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
32
Task/Caesar-cipher/Lua/caesar-cipher-2.lua
Normal file
32
Task/Caesar-cipher/Lua/caesar-cipher-2.lua
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
local memo = {}
|
||||
|
||||
local function make_table(k)
|
||||
local t = {}
|
||||
local a, A = ('a'):byte(), ('A'):byte()
|
||||
|
||||
for i = 0,25 do
|
||||
local c = a + i
|
||||
local C = A + i
|
||||
local rc = a + (i+k) % 26
|
||||
local RC = A + (i+k) % 26
|
||||
t[c], t[C] = rc, RC
|
||||
end
|
||||
|
||||
return t
|
||||
end
|
||||
|
||||
local function caesar(str, k, decode)
|
||||
k = (decode and -k or k) % 26
|
||||
|
||||
local t = memo[k]
|
||||
if not t then
|
||||
t = make_table(k)
|
||||
memo[k] = t
|
||||
end
|
||||
|
||||
local res_t = { str:byte(1,-1) }
|
||||
for i,c in ipairs(res_t) do
|
||||
res_t[i] = t[c] or c
|
||||
end
|
||||
return string.char(unpack(res_t))
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue