new tasks
This commit is contained in:
parent
2a4d27cea0
commit
80737d5a6a
1194 changed files with 15353 additions and 1 deletions
30
Task/Caesar_cipher/Lua/caesar_cipher.lua
Normal file
30
Task/Caesar_cipher/Lua/caesar_cipher.lua
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
local function encrypt(text, key)
|
||||
return text:gsub("%a", function(t)
|
||||
local base = (t:lower() == t and string.byte('a') or string.byte('A'))
|
||||
|
||||
local r = t:byte() - base
|
||||
r = r + key
|
||||
r = r%26 -- works correctly even if r is negative
|
||||
r = r + base
|
||||
return string.char(r)
|
||||
end)
|
||||
end
|
||||
|
||||
local function decrypt(text, key)
|
||||
return encrypt(text, -key)
|
||||
end
|
||||
|
||||
caesar = {
|
||||
encrypt = encrypt,
|
||||
decrypt = decrypt,
|
||||
}
|
||||
|
||||
-- test
|
||||
do
|
||||
local text = "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz"
|
||||
local encrypted = caesar.encrypt(text, 7)
|
||||
local decrypted = caesar.decrypt(encrypted, 7)
|
||||
print("Original text: ", text)
|
||||
print("Encrypted text: ", encrypted)
|
||||
print("Decrypted text: ", decrypted)
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue